Reputation: 730
I want to get all the words, except one, from a string using JS regex match function. For example, for a string testhello123worldtestWTF
, excluding the word test
, the result would be helloworldWTF
.
I realize that I have to do it using look-ahead functions, but I can't figiure out how exactly. I came up with the following regex (?!test)[a-zA-Z]+(?=.*test)
, however, it work only partially.
Upvotes: 0
Views: 131
Reputation: 3189
WORDS OF CAUTION
Seeing that you have no set delimiters in your inputted string, I must say that you cannot reliably exclude a specific word - to a certain extent.
For example, if you want to exclude test
, this might create a problem if the input was protester
or rotatestreet
. You don't have clear demarcations of what a word is, thus leading you to exclude test when you might not have meant to.
On the other hand, if you just want to ignore the string test
regardless, just replace test
with an empty string and you are good to go.
Upvotes: 0
Reputation: 199
Plugging this into your refiddle produces the results you're looking for:
/(test)/g
It matches all occurrences of the word "test" without picking up unwanted words/letters. You can set this to whatever variable you need to hold these.
Upvotes: 0
Reputation: 5850
Lookarounds seem to be an overkill for it, you can just replace the test
with nothing:
var str = 'testhello123worldtestWTF';
var res = str.replace(/test/g, '');
Upvotes: 0
Reputation: 584
IMHO, I would try to replace the incriminated word with an empty string, no?
Upvotes: 1