Xponex
asked on
Regular Expression Needed For finding words but not words inside HTML
I have a regular expression that simply replaces a given word with the the same word surrounded by a SPAN tag to highlight it, like this:
oRegExp.Pattern = "(" & aSearch(nTerm) & ")"
sArticle = oRegExp.Replace(sArticle," <span style=""background-color:# FFFF00"">$ 1</span>")
The problem is, if someone searches for something that is within an HTML tag, like if they search for SPAN, then it busts up the HTML. So... I need a new regular expression that only finds the keywords if they NOT with < > tags.
Am I wanting too must from a regular expression? Should I do this some other way that would be more efficient?
oRegExp.Pattern = "(" & aSearch(nTerm) & ")"
sArticle = oRegExp.Replace(sArticle,"
The problem is, if someone searches for something that is within an HTML tag, like if they search for SPAN, then it busts up the HTML. So... I need a new regular expression that only finds the keywords if they NOT with < > tags.
Am I wanting too must from a regular expression? Should I do this some other way that would be more efficient?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now, can you explain exactly WHY this works? I know that's not part of the question, but... I'd like to know for future knowledge.
Thanks for the question and the points.
I put "test" in the pattern and here's the explanation:
-------------------------- ---------- ---------- ---------- ---------- ----
test 'test'
-------------------------- ---------- ---------- ---------- ---------- ----
(?! look ahead to see if there is not:
-------------------------- ---------- ---------- ---------- ---------- ----
[^<>]* any character except: '<', '>' (0 or
more times (matching the most amount
possible))
-------------------------- ---------- ---------- ---------- ---------- ----
> '>'
-------------------------- ---------- ---------- ---------- ---------- ----
) end of look-ahead
-------------------------- ---------- ---------- ---------- ---------- ----
I put "test" in the pattern and here's the explanation:
--------------------------
test 'test'
--------------------------
(?! look ahead to see if there is not:
--------------------------
[^<>]* any character except: '<', '>' (0 or
more times (matching the most amount
possible))
--------------------------
> '>'
--------------------------
) end of look-ahead
--------------------------
ASKER