我怎样才能只提取以数字结尾的单词?

时间:2015-01-06 13:00:23

标签: javascript regex

我从后端获取信息。我需要将其转换为多个单词,并要求存储在数组中。使用regexp如何获得?

这是单词:

Enter Room/Area^_^Area 100#_#Enter Grid^_^Grid2#_#Enter Level / Building^_^Building1#_#Enter Drawing^_^Drawing1#_#Enter Spec section^_^Spec2#_#Enter BOQ^_^BOQ1#_#

结果应该是:

["Area 100", "Grid2","Building1", "Drawing1", "Spec2", "BOQ1" ]

所以我只想选择以数字结尾的单词。到特殊字符^

1 个答案:

答案 0 :(得分:3)

\b[a-zA-Z0-9 ]+\d+\b

试试这个。看看演示。

https://regex101.com/r/wZ0iA3/6

var re = /\b[a-zA-Z0-9 ]+\d+\b/gi;
var str = 'Enter Room/Area^_^Area 100#_#Enter Grid^_^Grid2#_#Enter Level / Building^_^Building1#_#Enter Drawing^_^Drawing1#_#Enter Spec section^_^Spec2#_#Enter BOQ^_^BOQ1#_#';
var m;

while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}