MirrorMirror
MirrorMirror

Reputation: 188

javascript regex match word not followed by pattern with text inbetween

text:

id="word1 word2 word3/343/els/425/word4 word5 word6"
wordA wordB Bword2B word1 word2 word3
blah blah blah

i'm trying to match the whole words: word1, word2, word3 in the text BUT i don't want them matched in the id="word1 word2 word3/343/els/425/word4 word5 word6" line of the text, the words are not in latin.

current approach:

var sWord = "word2";
text = text.replace( new RegExp(sWord+"(?!\/[0-9])", "gi"), "<span style='background-color: yellow'>"+sWord+"</span>");

this approach only works for word3, not for word1 or word2, so how do I introduce in the regex the possibility that before the NUMBER,there is another word?

"(?!.\/[0-9])"

i added the dot "." to the regex. but it doesn't seem to work.

Plus, how do I add the possibility for whole word match? \b and \S don't seem to work, because words are not in latin.

thank you in advance!

EDIT: jsfiddle http://jsfiddle.net/GLt4F/5/

as you can see the button breaks for word1 and word2 but not for word3 because the match is correct

EDIT2:

rephrasing the problem: match AAA ONLY if not followed by BBB and between them can be anything. for example:

AAA frefe BBB should not be matched
AAA BBB should not be matched
AAA frefe cc should be matched

Upvotes: 0

Views: 1859

Answers (2)

MirrorMirror
MirrorMirror

Reputation: 188

(?!(.*)\/[0-9])

this seems to do the trick ( for not matching when something is in between. don't know if there are loopholes or if it's a solid answer though.

Upvotes: 0

Mike Samuel
Mike Samuel

Reputation: 120486

/AAA([\s\S](?!=BBB))*$/

should do it.

Plus, how do I add the possibility for whole word match? \b and \S don't seem to work, because words are not in latin.

JavaScript doesn't have a unicode compatibility mode, so you have to write your own character set for a word char and use negative matching, so

(?:^|[^A-Za-z0-9_$])

before the AAA, and there's no lookbehind in JS so the previous non-word char will be part of captured group 0.

Upvotes: 3

Related Questions