使用正则表达式从javascript中的字符串中获取值数组

时间:2016-10-27 11:06:01

标签: javascript jquery regex string

我正在尝试使用javascript来获取字符串内的所有单词,但我不明白。请帮帮我。

输入

    VM505:2 Uncaught TypeError: Cannot read property 'push' of undefined(…)

所需输出

"  (  [Field1]  +  [Field2]  )    +    (  [Field3]  -  [Field4]  )  "

提前致谢

1 个答案:

答案 0 :(得分:2)

您可以轻松使用String.prototype.match()通过正则表达式选择字符串的特定部分。请注意,\w+与单词字符匹配。

var str =  "  (  [Field1]  +  [Field2]  )    +    (  [Field3]  -  [Field4]  )  ";
var result = str.match(/\w+/g);
console.log(result);