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.


SiriusPhilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rob SiklosCommented:
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.
0
b0lsc0ttIT ManagerCommented:
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
0
b0lsc0ttIT ManagerCommented:
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
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

hieloCommented:
Try this:
/[<]cfquery[^>]*[>][^<]+[<][\/]cfquery>/ig
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b0lsc0ttIT ManagerCommented:
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.
0
hieloCommented:
>>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 :)
0
b0lsc0ttIT ManagerCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.