• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

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.


0
SiriusPhil
Asked:
SiriusPhil
  • 4
  • 2
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
hieloCommented:
Try this:
/[<]cfquery[^>]*[>][^<]+[<][\/]cfquery>/ig
0
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now