I am trying to highlight a set of keywords using JavaScript and a regular expression, I ran into one problem, my keyword may contain literals and special characters, like in @text #number, etc. I use the word boundary to match and replace the whole word and not a partial word (contained in another word).
var pattern = new regex('\b '( + keyword +')\b',gi);
Here, this expression matches all keywords and selects them, however, in the event that any keyword, for example, "number:", is not highlighted.
I know that \bword\b matches the word boundary, and special characters are not alphanumeric characters, so they do not match the above expression. Can you tell me which regular expression I can use to accomplish the above.
== Update ==
For the above, I tried Tim Pitzker's suggestion for lower regular expression,
expr: (?:^|\\b|\\s)(" + keyword + ")(?:$|\\b|\\s)
The above seems to work to get a match for the whole word with alphanumeric and non-alphanumeric characters, however whenever a keyword has a sequential html tag before or after a keyword without a space, it does not highlight that keyword (e.g. , social security * number: <br> *) I tried the following regular expression, but it replaces the html tag preceding the keyword
expr: (?:^|\b|\s|<[^>]+>)number:(?:$|\b|\s|<[^>]+>)
Here, for the keyword, the number:, which has < br > (the space specially designed for the br label to avoid the browser interpreting the tag), the next one without a space between them, is highlighted by the keyword.
Can you suggest an expression that ignores the sequential html tag for the entire word with alphanumeric and non-alphanumeric characters.