views:

66

answers:

1

I am working on a small app where I need to remove the starting and ending
tags and I am having a little trouble getting the expression right.

Currently I have this bit of code. The issue is on the second output, nothing is displayed.

<cfcontent reset="true"/>
<cfset myStr = '<br> <br> <br> <br> This is a great Test<br> do you like my test? <br><br><br>'>


<cfoutput>#myStr#</cfoutput>

<cfset myNewString = REReplaceNoCase(myStr, '(^<.*?>+)|(<.*?>+$)', '' ,'ALL')>

<cfoutput>New: #myNewString#</cfoutput>
+1  A: 

The following regex worked for me:

(^<[^>]*?>+)|(<[^>]*?>+$)

It removed the first and last tag if that's what you wanted.

However, the + after the closing angle bracket suggests that you maybe meant to remove all tags at the start or end; although in the current form it would match one or more closing angle brackets. You need to use groups to change that behavior:

(^(<[^>]*?>\s*)+)|((<[^>]*?>\s*)+$)

This removes all tags at the start or end of the string.

Joey
Worked like a charm. Thank you for your help.
Tempname
You're welcome. I didn't quite figure out why your variant removed everything, though.
Joey