473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
5 1366
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Ed Brown | last post by:
I'm working on a VB.Net application that needs to do quite a bit of string pattern matching, and am having problems using the "LIKE" operator to match the same string twice in the pattern. For...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
18
by: Umesh | last post by:
Do you have any answer to it? thx.
2
by: =?Utf-8?B?QWFyb24=?= | last post by:
Hi, I'm having a tricky problem where I want to accept a regular expression pattern from user input but can't get teh escape characters to be prcoessed correctly. If I take the same pattern and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.