如何使用正则表达式从具有状态 "notify" 或 "not in stock" 的字符串中删除项目
How do I remove items from a string which have status "notify" or "not in stock" using regexp
我正在努力制作一个正则表达式来删除状态为“通知”或“无货”的商品
item1 120usd not in stock item2 150usd in stock item3 100usd notify item4 12usd in stock item5 25usd not in stock item6 250usd notify item7 50usd in stock item8 30sud in stock item9 5usd notify item10 5usd notify
以下正则表达式将匹配所有项目
.*?(notify|not in stock|in stock)
我试图从正则表达式中删除“有货”,但随后所有的分组都“混乱”了。
https://regex101.com/r/KyEg6k/1
感谢所有帮助:)
一种选择是匹配您不想要的内容,并在一组中捕获您想要保留的内容。
为了不超过 in stock
或 not in stock
或 notify
,您可以使用 使用负前瞻。
\bin stock\b|(?:\s+|^)((?:(?!\b(?:notify|not in stock|in stock)\b).)+\b(?:notify|not in stock)\b)
\bin stock\b
在单词边界之间匹配 in stock
以防止部分匹配
|
或
(?:\s+|^)
匹配 1+ 个空白字符或断言字符串的开头也匹配第一个单词
(
捕获组1(示例代码中引用m[1]
)
(?:
钢化点非捕获组
(?!\b(?:notify|not in stock|in stock)\b).
Negative lookahead,断言不是直接向右的任何备选方案。如果是,则使用 .
匹配任何字符
)+
关群重复1+次
\b(?:notify|not in stock)\b
匹配单词边界之间的备选方案之一
)
关闭组 1
const str = "item1 120usd not in stock item2 150usd in stock item3 100usd notify item4 12usd in stock item5 25usd not in stock item6 250usd notify item7 50usd in stock item8 30sud in stock item9 5usd notify item10 5usd notify"
const regex = /\bin stock\b|(?:\s+|^)((?:(?!\b(?:notify|not in stock|in stock)\b).)+\b(?:notify|not in stock)\b)/g;
Array.from(str.matchAll(regex), m => {
if (m[1]) {
console.log(m[1]);
}
});
我正在努力制作一个正则表达式来删除状态为“通知”或“无货”的商品
item1 120usd not in stock item2 150usd in stock item3 100usd notify item4 12usd in stock item5 25usd not in stock item6 250usd notify item7 50usd in stock item8 30sud in stock item9 5usd notify item10 5usd notify
以下正则表达式将匹配所有项目
.*?(notify|not in stock|in stock)
我试图从正则表达式中删除“有货”,但随后所有的分组都“混乱”了。
https://regex101.com/r/KyEg6k/1
感谢所有帮助:)
一种选择是匹配您不想要的内容,并在一组中捕获您想要保留的内容。
为了不超过 in stock
或 not in stock
或 notify
,您可以使用
\bin stock\b|(?:\s+|^)((?:(?!\b(?:notify|not in stock|in stock)\b).)+\b(?:notify|not in stock)\b)
\bin stock\b
在单词边界之间匹配in stock
以防止部分匹配|
或(?:\s+|^)
匹配 1+ 个空白字符或断言字符串的开头也匹配第一个单词(
捕获组1(示例代码中引用m[1]
)(?:
钢化点非捕获组(?!\b(?:notify|not in stock|in stock)\b).
Negative lookahead,断言不是直接向右的任何备选方案。如果是,则使用.
匹配任何字符
)+
关群重复1+次\b(?:notify|not in stock)\b
匹配单词边界之间的备选方案之一
)
关闭组 1
const str = "item1 120usd not in stock item2 150usd in stock item3 100usd notify item4 12usd in stock item5 25usd not in stock item6 250usd notify item7 50usd in stock item8 30sud in stock item9 5usd notify item10 5usd notify"
const regex = /\bin stock\b|(?:\s+|^)((?:(?!\b(?:notify|not in stock|in stock)\b).)+\b(?:notify|not in stock)\b)/g;
Array.from(str.matchAll(regex), m => {
if (m[1]) {
console.log(m[1]);
}
});