regex - Javascript regular expression prevent matching inside tags -
I have to match the string that is not inside the tags. I am working on projects that I have a back-end There is no control over the HTML rendering code. What I need to do is to add a hover functionality to many dynamic words. I have created a script that will search for those key words in specific elements and add their description to the title tag for hoverMy problem is that if other keywords are found in the title tags of the second keyword.
My JS:
var str = 'match & lt; Span title = "no match here" & gt; Match & lt; / Span & gt; Match '; Str.replace (/ match / gym, 'okay');
I do not want to change the word "match" in title matching, my desired result is:
'ok & lt; Span title = "no match here" & gt; OK & lt; / Span & gt; OK '
How can I do this with javascript?
I tried the expression below but it is not working for me:
^ ((!! (". +") Match) * $
You need to be able to avoid tag first to capture them:
var result = str.replace (/ (<[^> gt; * & gt;) | mail / gi, function (_, g1) {return (g1 == undefined)? OK: g1;});
But if you can, using DOM is probably the best method.
Comments
Post a Comment