我正在尝试使用javascript来获取字符串内的所有单词,但我不明白。请帮帮我。
输入
VM505:2 Uncaught TypeError: Cannot read property 'push' of undefined(…)
所需输出
" ( [Field1] + [Field2] ) + ( [Field3] - [Field4] ) "
提前致谢
答案 0 :(得分:2)
您可以轻松使用String.prototype.match()
通过正则表达式选择字符串的特定部分。请注意,\w+
与单词字符匹配。
var str = " ( [Field1] + [Field2] ) + ( [Field3] - [Field4] ) ";
var result = str.match(/\w+/g);
console.log(result);