Link to home
Start Free TrialLog in
Avatar of SiriusPhil
SiriusPhil

asked on

Need a RegEx to match <cfquery sometext>somemore text</cfquery>... but...

I need a RegEx to match <cfquery sometext>somemore text</cfquery>.  But here is the trick.  I cannot use </cfquery> without alteration in the regex because it is being use in a javascript on a ColdFusion page.  The javascript with the regular expression using </cfscript>  will cause a ColdFusion error.


500 points to the person who supplies me with the proper regex.


Avatar of Rob Siklos
Rob Siklos
Flag of Canada image

if you need the regex in JS, then do something like this:

var tagName = "cfquery";
regex = new RegExp("<" + tagName + "\\s+[^>]*>[^<]*</" + tagName + ">";

Then, you can use regex.test(), or regex.exec(), to do whatever you need.
Avatar of b0lsc0tt
SiriusPhil,

If you just need to make the expression safe for Javascript then use ...

result = subject.match(/<cfquery[^>]*>[^<]+<\/cfquery>/);

Notice the backslash before the forward slash in the closing tag.

Let me know if you have any questions or need more information.

b0lsc0tt
SiriusPhil,

By the way ... if you need to not use the forward slash at all then try ...

<cfquery[^>]*>[^<]+<\x2Fcfquery>

In an expression \x2F is the same as the / character.

Let me know if you have a question.

b0lsc0tt
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If case will be an issue and/or there may be more than one match then the i and/or g switch used in Hielo's comment should be added.  The rest of the expression is exactly like the one I suggested in http:#20860208 with some unnecessary square brackets (or classes) added.  Hielo, let me know if you don't know why I say they aren't needed.  Only the 2 I had in my expression are required and the others, since they only have a single character, should be omited.

bol

p.s.  I hope you didn't just add them to make the expression look different. ;)  The /ig suggestion alone was worth a post in case the Asker needed it and you could've just given credit to my comment.
>>with some unnecessary square brackets (or classes) added.  Hielo, let me know if you don't know why I say they aren't needed.
The "unnecessary" brackets are added for 'safety' reasons. <cfquery is a coldfusion "keyword". Wrapping the "<" in brackets will avoid any potential ambiguity bug in the cf parser. Basically I am making it clear to the CF parser that that is  be treated as "literal" text ie "Don't interpolate". I hava faith that parser coders will screw up. I've seen it time and again. For the correct parser implementations, the brackets are not necessary. For I tend to focus/worry about those buggy programs and avoid them if possible. That technique has saved me tons of headaches. I know why you say they aren't needed. I guess you didn't know why I added them.

>>I hope you didn't just add them to make the expression look different"
No I did not. I suppose that is clear now

Take care :)
Hielo,

Thanks for the reply and info on why you added them.  Interesting.  I wish you would've mentioned that in the other post; I wouldn't have made my last comment at all.

I haven't used CF and from what you have said I am glad.  I hope it isn't really as tempermental as you said but I agree that it is best to be careful.  All languages have the little issues. :)  Thanks again for explaining.

bol