?
Solved

How to check a regular expression string is valid or not?

Posted on 2006-06-13
3
Medium Priority
?
236 Views
Last Modified: 2008-03-10
I try to write a program that allow people to input an regular expression and then I will use it to excute. But If the input string is not a valide regular expression then when execute it, the executer will through error.
It's not good. And I wanna check the valide of it!

So please show me how to do that!
Thanks so much!
0
Comment
Question by:tunhien
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 16896721
0
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 16896762

Maybe there is an easier way....  try using a  "try .. catch..."  block when you create the regex object:

try {

var re = new RegExp(document.yourFormName.regexTextBox.value);
}
catch {
  alert("that's some funky regex dude.");
}
0
 
LVL 10

Accepted Solution

by:
0h4crying0utloud earned 500 total points
ID: 16896902

Check this out:  I added the try..catch to another script I use very often:

http://www.regular-expressions.info/javascriptexample.html

They didn't do error checking but this seems to work now:

<SCRIPT LANGUAGE="JavaScript"><!--
function demoMatchClick() {
      
 try {
  var re = new RegExp(document.demoMatch.regex.value);
      }
      catch(e) {
        alert("Error in regex");
            return;
      }      
      
  if (document.demoMatch.subject.value.match(re)) {
    alert("Successful match");
  } else {
    alert("No match");
  }
}

function demoShowMatchClick() {
 try {
  var re = new RegExp(document.demoMatch.regex.value);
      }
      catch(e) {
        alert("Error in regex");
            return;
      }
  var m = re.exec(document.demoMatch.subject.value);
  if (m == null) {
    alert("No match");
  } else {
    var s = "Match at position " + m.index + ":\n";
    for (i = 0; i < m.length; i++) {
      s = s + m[i] + "\n";
    }
    alert(s);
  }
}

function demoReplaceClick() {
  var re = new RegExp(document.demoMatch.regex.value, "g");
  document.demoMatch.result.value =
    document.demoMatch.subject.value.replace(re,
      document.demoMatch.replacement.value);
}
// -->
</SCRIPT>

<FORM ID="demoMatch" NAME="demoMatch" METHOD=POST ACTION="javascript:void(0)">
<P>Regexp: <INPUT TYPE=TEXT NAME="regex" VALUE="\bt[a-z]+\b" SIZE=50></P>
<P>Subject string: <INPUT TYPE=TEXT NAME="subject"
   VALUE="This is a test of the JavaScript RegExp object" SIZE=50></P>
<P><INPUT TYPE=SUBMIT VALUE="Test Match" ONCLICK="demoMatchClick()">
<INPUT TYPE=SUBMIT VALUE="Show Match" ONCLICK="demoShowMatchClick()"></P>

<P>Replacement text: <INPUT TYPE=TEXT NAME="replacement" VALUE="replaced" SIZE=50></P>
<P>Result: <INPUT TYPE=TEXT NAME="result"
   VALUE="click the button to see the result" SIZE=50></P>
<P><INPUT TYPE=SUBMIT VALUE="Replace" ONCLICK="demoReplaceClick()"></P>
</FORM>
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question