« Back to code index... | Contact me regarding this code...

Replacing text between, not within, tags

View the code for this demo.

Note: The regex rule for this replacement is based on the rules defined under code sample Using regex to find tags without a trailing slash.

The purpose of this example is to show how you could, using Cold Fusion, replace text within a block of HTML but only affect the text that is visible and not part of any HTML tag.


Replace This:

With This:

Output with replaced text

<a href="/meta/RSS.xml > test</a>" title="My RSS feed">Our Really Simple Syndication 2.0 feed</a>

The > in the href attribute of the above anchor tag is invalid, 
but I want to show how this code doesn't cause the R.S.S. in
the title attribute to be replaced and how the code isn't tricked
by the extra closing anchor tag (</a>) within the href attribute.

5 < 10 - Really Simple Syndication > 15

This is to demonstrate that random opening characters won't cause the 
code to fail to replace, since it only excludes properly formatted
HTML tags.

<br class="RSS">

<img src="RSS.jpg" title="RSS" /><br />

This is our <strong class="RSS">Really Simple Syndication</strong> feed.

Valid XHTML 1.1!