By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jan 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ivo
"lwoods" <la***@lwoods.com> wrote
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.
--
hth
ivo
http://4umi.com/web/javascript/
Jan 10 '06 #2

P: n/a
lwoods wrote:
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);

--
Rob
Jan 11 '06 #3

P: n/a
Thanks, Rob

Will do...

Larry

"RobG" <rg***@iinet.net.au> wrote in message
news:te****************@news.optus.net.au...
lwoods wrote:
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);

--
Rob

Jan 11 '06 #4

P: n/a
I shoulda' known better...

Thanks

Larry

"Ivo" <no@thank.you> wrote in message
news:43*********************@news.wanadoo.nl...
"lwoods" <la***@lwoods.com> wrote
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.
--
hth
ivo
http://4umi.com/web/javascript/

Jan 11 '06 #5

P: n/a
lwoods wrote:
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

Jan 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.