Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression help req'd.

Status
Not open for further replies.

Kendo

Programmer
Jun 23, 2000
28
GB
Hi all,

Ugh! Regular expressions...no matter how many people say "Oh, they're okay once you get used to them..." I can't seem to get used to them.

To be honest, I'm struggling to find any CF based tutorials.

Basically what I want to do is replace content between two quotation marks and add HTML tags to either side, eg.

"This is my beautiful text"

to

<span style'color:black'>&quot;This is my beautiful text&quot;</span>

Is this possible? Am I living in a world of pipedreams?!

Who knows, but if it is possible I'd be very grateful for any help.

PS. As a bonus point, say I wanted it to do the above but NOT in circumstances when the quotation marks are inside a tag (ie. between angle brackets)? Is this possible?

Again, thanks! :D
 
Try this:
Code:
<cfoutput>
#REReplaceNoCase(string,'([^=])(&quot;[^&quot;]*&quot;)','\1<span style=&quot;color:black;&quot;>\2</span>',ALL)#
</cfoutput>
A step-by-step explanation might be of use to you:

REReplaceNoCase() is the function that replaces using regular expressions, and doesn't care about case.

string is the name of the variable that holds the string you want to change (or it could be just a string).

Here's the regular expression, step by step:

([^=]) matches any single character but the equals sign and stores the resulting match in a temporary variable. This is a little hack to prevent your quoted attributes inside tags from being matched, but only if you don't put a space between the equals sign and the opening quotes.

(&quot;[^&quot;]*&quot;) matches a string of zero or more characters inside quotation marks, and stores the resulting match in another temporary variable.

Here's the replacement string, step by step:
\1 inserts the value of the first temporary variable. This is necessary to avoid losing that character that we made sure wasn't an equals sign.

<span style=&quot;color:black;&quot;> is the text you want to insert before the main string that was matched.

\2 inserts the value of the second temporary variable. This is the main string, including the quotation marks.

</span> is the text you want to insert after the main string.

If you need any more information, please post back. I understand how confusing regular expressions can be when you're just learning them. Once you get the hang of it, you'll never go back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top