I am struggling to make a regular expression to remove the items which have the status "notify" or "not in stock"
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
The following regular expression will match all items
.*?(notify|not in stock|in stock)
I have tried to remove the "in stock" from the regular expression but then all the grouping get "messed" up.
https://regex101.com/r/KyEg6k/1
All help is appreciated :)
One option could be to match what you don't want and capture in a group what you want to keep.
To not cross in stock
or not in stock
or notify
you could use a tempered greedy token using a negative lookahead.
\bin stock\b|(?:\s+|^)((?:(?!\b(?:notify|not in stock|in stock)\b).)+\b(?:notify|not in stock)\b)
\bin stock\b
match in stock
between word boundaries to prevent a partial match|
Or(?:\s+|^)
Match either 1+ whitespace chars or assert the start of the string to also match the first word(
Capture group 1 (Referred to by m[1]
in the example code)
(?:
Non capture group for the tempered dot
(?!\b(?:notify|not in stock|in stock)\b).
Negative lookahead, assert not any of the alternatives directly to the right. If that is true, match any char using the .
)+
Close the group and repeat 1+ times\b(?:notify|not in stock)\b
Match one of the alternatives between word boundaries)
Close group 1const 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]);
}
});