簡體   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