Search code examples
regexcoldfusioncoldfusion-9

ColdFusion 9: Strip HTML tags out of specific parts of a string


I've been cracking my head over this for quite some time.

I currently build a custom BB-Code function as part of a project at work. But I don't get it to work at one point: a [code] block. Using ColdFusion regex, I want to replace the < and > characters with &lt; and &gt;, but only on HTML betweeen the [code] blocks. So, how can I restrict a regex expression to the part of the string which is between the [code] blocks. Thanks in advance for any help.


Solution

  • I think in ColdFusion you'll have to iterate over the string, searching for occurrences of "[code]". When you find such an occurrence, read in the string until you hit "[/code]". Take that string and do a replaceList to replace the characters. Use the removeChars and insert functions to replace the old string with the new. One problem with using regular expressions in this context is that the CF function REReplace can't replace a pattern with another pattern, only with a string.