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
/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.
(?!(.*)\/[0-9])
这似乎可以解决问题(因为介于两者之间时不匹配。不知道是否存在漏洞,或者这是否是一个可靠的答案。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.