符合正则expression式的node.js / strings
我在node.js中有一个string列表
var listStr = ["helloworld","helloworld1","helloworld2","somethingelse"]
我想find所有符合一些正则expression式的string。 例如,对于正则expression式: *world*
,我会得到下一个string: "helloworld","helloworld1","helloworld2"
有任何npm包或函数获得一个string和正则expression式的列表,并返回符合正则expression式的string?
你不需要这个NPM包:
var filtered = listStr.filter(function (item) { return item.match(/world/); });
filtered
内容:
[ 'hello world', 'helloworld1', 'helloworld2' ]
你可以用普通的JavaScript来做到这一点。 只需修复你的正则expression式,并使用Array#filter()
:
listStr.filter(function(str){return /.*world.*/.test(str)}) ^^ ^^ proper wildcard