Solved

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

Posted on 2006-06-13
3
212 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Refresh div via ajax without lost ccs <li> styles 8 37
HTML5 Looping table certain Cell 11 44
Why doesn't Google directions work? 6 48
Else condition 9 19
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
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…

831 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