views:

573

answers:

6

I am designing a regular expression tester in html and javascript. The user will enter a regex, a string, and choose the function they want to test with (eg search, match, replace, etc. ) via radio button and the program will display the results when that function is run with the specified arguments. Naturally there will be extra text boxes for the extra arguments to replace and such.

My problem is getting the string from the user and turning it into a regular expression. If I say that they don't need to have //'s around the regex they enter, then they can't set flags, like g and i. So they have to have the //'s around the expression, but how can I convert that string to a regex? It can't be a literal since its a string, and I can't pass it to the RegExp constructor since its not a string without the //'s. Is there any other way to make a user input string into a regex? Will I have to parse the string and flags of the regex with the //'s then construct it another way? Should I have them enter a string, and then enter the flags separately? Thanks guys!

PS I also noticed that when getting the value of a textbox you have to use textbox.value and not textbox.getAttribute("value"). Is this because the XML getAttribute is looking for an attribute in the code, and not getting the actual user entered value of the textbox?

+2  A: 

Use the JavaScript RegExp object constructor.

var re = new RegExp("\\w+");
re.test("hello");

You can pass flags as a second string argument to the constructor. See the documentation for details.

Ayman Hourieh
+6  A: 

Use the RegExp object constructor to create a regular expression from a string:

var re = new RegExp("a|b", "i");
// same as
var re = /a|b/i;
Gumbo
+1  A: 

If this is meant to be a GUI regex tester, then provide check boxes for the various options.

Chas. Owens
+2  A: 

I suggest you also add separate checkboxes or a textfield for the special flags. That way it is clear that the user does not need to add any //'s. In the case of a replace, provide two textfields. This will make your life a lot easier.

Why? Because otherwise some users will add //'s while other will not. And some will make a syntax error. Then, after you stripped the //'s, you may end up with a syntactically valid regex that is nothing like what the user intended, leading to strange behaviour (from the user's perspective).

Stephan202
+1  A: 

You can ask for flags using checkboxes then do something like this:

var userInput = formInput;
var flags = '';
if(formGlobalCheckboxChecked) flags += 'g';
if(formCaseICheckboxChecked) flags += 'i';
var reg = new RegEx(userInput, flags);
Pim Jager
+1  A: 
var flags = inputstring.replace(/.*\/([gimy]*)$/, '$1');
var pattern = inputstring.replace(new RegExp('^/(.*?)/'+flags+'$'), '$1');
var regex = new RegExp(pattern, flags);

or

var match = inputstring.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
// sanity check here
var regex = new RegExp(match[0], match[1]);
Anonymous
You should consider that an invalid input like `/\/` is recognized.
Gumbo
Or let the RegExp constructor fail, "trailing \ in regular expression", instead of writing a complicated parser.
Anonymous