繁体   English   中英

Javascript正则表达式-以任意顺序包含列表中的单词和单词

[英]Javascript Regex - contains word AND word from list in any order

我需要找到一个特定的单词以及列表中的所有单词。 到目前为止,我有这个:

^.*?(apple)(?:\W+\w+){0,30}?\W+(ipad|itunes).*

这与“苹果”和“ ipad”或“ itunes”匹配。

但是,如果“ ipad”或“ itunes”在“ apple”之前,则此操作失败。

有人可以建议吗?

您最好为此提前使用:

/^(?=.*?\bapple\b)(?=.*?\b(ipad|itunes)\b).*$/i

更新:根据OP的评论: Can you advise how my word limit would fit in here? eg I must find any from the list within 20 words of apple? Can you advise how my word limit would fit in here? eg I must find any from the list within 20 words of apple?

/^(?=.*?\bapple\b)(?=.*?\bapple(\W+\w+){0,20}\b(ipad|itunes)\b).*$/i

简单版本( http://jsfiddle.net/xhdBQ/1/ )不起作用吗?

var list = ["ipad", "itunes"];
var word = "apple";
list.push(word)

var regex = new RegExp("(" + list.join("|") + ")", "g");
console.log(regex);
console.log("blabla...apple,,safsd ssdfipadaad itunes".match(regex))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM