Solved

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

Posted on 2006-06-13
3
228 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 125 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

689 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