如何在 JavaScript 中区分特殊字符?
javascriptweb developmentobject oriented programming更新于 2025/10/30 1:16:17
要区分特殊字符,请使用正则表达式的 match() 概念。语法如下 −
yourStringName.flatMap(anyVariableName => yourVariableName.match(/\w+|\W+/g));
假设,以下是值之间有特殊字符的数组 −
var allNames = ['John-Smith', 'David', 'Carol%Taylor'];
现在让我们看看如何用特殊字符分隔文本。以下是代码 −
示例
var allNames = ['John-Smith', 'David', 'Carol%Taylor']; var output = allNames.flatMap(obj => obj.match(/\w+|\W+/g)); console.log(output);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,文件名是 demo32.js。
输出
这将产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo32.js [ 'John', '-', 'Smith', 'David', 'Carol', '%', 'Taylor' ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

