Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now