How do I use a string as a reg exp pattern?

I

Ivo

lwoods said:
I am trying to use a string for a regular expression pattern. Here is a
test that I set up:

var s="^\d{3}-\d{3}-\d{4}$";
var r=new RegExp();
r.compile(s);
var t="333-333-3333";
alert(r.test(t));

I have tried this with and without the "compile", and also adding the
"/..../" to the beginning and end of the string, but I always get "false".

Can you use a string to represent a pattern...in Javascript?

Yes, and as is usual with strings, it will look for and evaluate backslashes
which it thinks introduce an escaped meta-character in the next position. So
if you want your backslashes to even be seen by the regex interpreter, you
need to ... well, escape them. Try this:

var s="^\\d{3}-\\d{3}-\\d{4}$";

Every two backslahes become one backslash in the regex. Don't surround the
string with forward slashes. Given a string, the regexer will do that
itself.
 
R

RobG

lwoods said:
I am trying to use a string for a regular expression pattern. Here is a
test that I set up:

var s="^\d{3}-\d{3}-\d{4}$";
var r=new RegExp();
r.compile(s);
var t="333-333-3333";
alert(r.test(t));

What Ivo said, plus you can ditch the compile line with:

var r = new RegExp(s);
 
L

lwoods

I am trying to use a string for a regular expression pattern. Here is a
test that I set up:

var s="^\d{3}-\d{3}-\d{4}$";
var r=new RegExp();
r.compile(s);
var t="333-333-3333";
alert(r.test(t));

I have tried this with and without the "compile", and also adding the
"/..../" to the beginning and end of the string, but I always get "false".

Can you use a string to represent a pattern...in Javascript?

TIA,

Larry Woods
 
L

lwoods

I shoulda' known better...

Thanks

Larry

Ivo said:
Yes, and as is usual with strings, it will look for and evaluate
backslashes
which it thinks introduce an escaped meta-character in the next position.
So
if you want your backslashes to even be seen by the regex interpreter, you
need to ... well, escape them. Try this:

var s="^\\d{3}-\\d{3}-\\d{4}$";

Every two backslahes become one backslash in the regex. Don't surround the
string with forward slashes. Given a string, the regexer will do that
itself.
 
M

mick white

lwoods said:
I am trying to use a string for a regular expression pattern. Here is a
test that I set up:

var s="^\d{3}-\d{3}-\d{4}$";
var r=new RegExp();
r.compile(s);
var t="333-333-3333";
alert(r.test(t));


alert(/^\d{3}\-\d{3}\-\d{4}$/.test("333-333-3333"));
Mick

I have tried this with and without the "compile", and also adding the
"/..../" to the beginning and end of the string, but I always get "false".

Can you use a string to represent a pattern...in Javascript?

TIA,

Larry Woods
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top