views:

124

answers:

2

Is it possible to ignore the html elements when doing a replace?

Sample code:

$myText.replace(new RegExp( $searchString, 'gi' ), '<span class="highlight">'+ $searchString + '</span>');

$myText is a large string of html e.g.

var $myText = "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, <img src="something">when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, <a href="#" title="Lorem">but</a> also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>"

$searchString is equal to whatever a user types into an text input box.

Update:

If yes, how would I do it given the above sample code?

+4  A: 

Yes, take a look at the following forum post:

http://forums.asp.net/t/1443955.aspx

The RegEx pattern you are looking for would be something similar to the following:

"(?<!<[^>]*)Jon Doe(?<![^>]*<)"

Basically, you're doing a search and replace on anything that lives outside brackets <>.

JavaScript:

phrase = phrase.replace(/"(?<!<[^>]*)Jon Doe(?<![^>]*<)" /i, "is not");
tambler
In jQuery terms, you would use the above RegEx expression in the following manner:phrase = phrase.replace(/"(?<!<[^>]*)Jon Doe(?<![^>]*<)"/i, "is not");
tambler
but won't a stray > in the text mess everything up? or malformed html where they just use a <p> in one instance and a <p>text</p> elsewhere?
Chad
Nope. Your only ignoring content that is within a tag - <ignore this here>don't ignore this</ignore this too>
tambler
unless for some reason they decided to include html code as part of their text input.
jellyfishtree
I have tried it a few times keep getting an invalid quantifier ?<!<[^>]*)Jon Doe(?<![^>]*<)" error from Firebug.when using this:$myText.replace(/"(?<!<[^>]*)Jon Doe(?<![^>]*<)" /i, '<span class="mdHighlight">'+ $searchString + '</span>');
The Guy
A: 

Parsing HTML with a regular expression? This isn't a good idea. I would suggest inserting the HTML into the DOM and then traverse the nodes.

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

Tim Down