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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx Validation

Status
Not open for further replies.

ggriffit

Programmer
Oct 25, 2002
1,578
GB
Dear All,
I am looking for a way to parse a RegEx entered by the user to ensure that it is a valid RegEx e.g. :

\d{2,4} = pass
\d{2,4 = fail

the standard functions listed for a RegEx don't seem to have this functionality, any ideas ?

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Hi

I would just let it go. Then the JavaScript interpreter will observe if there is a problem anyway.
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]checkregexp[/color][teal]([/teal]expr[teal],[/teal]flag[teal])[/teal]
[teal]{[/teal]
  [b]try[/b] [teal]{[/teal]
    [b]new[/b] [COLOR=darkgoldenrod]RegExp[/color][teal]([/teal]expr[teal],[/teal]flag[teal])[/teal]
    [b]return[/b] [b]true[/b]
  [teal]}[/teal] [b]catch[/b] [teal]([/teal]e[teal])[/teal] [teal]{[/teal]
    [b]return[/b] [b]false[/b]
  [teal]}[/teal]
[teal]}[/teal]
Note that JavaScript accepts /\d{2,4/ as valid regular expression.

Feherke.
 
Feherke,
Thanks for that, was hoping that there would be some kind of parse test, but it looks like I'll just have to keep my fingers crossed and assume the RegEx's are tested before being added.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Feherke,
The problem, as you stated is that the JS RegEx object accepts \d{2, as a valid RegEx as opposed to failing it.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Hi

Feherke said:
Note that JavaScript accepts /\d{2,4/ as valid regular expression.
Note that Perl, PHP and Python also accepts /\d{2,4/ as valid regular expression.

I would say, you actually not only want to syntactically check the regular expression, but also to guess if is the same as the visitor wishes.


Feherke.
 
Greg said:
any ideas ?

I can think of two options:

1) Write your own client-side regex validator

2) Find a server-side language that can parse regular expressions for "validity" and submit the regex to it (via a form submission or AJAX, etc)

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top