/    Sign up×
Community /Pin to ProfileBookmark

Using stored RegExp String Value

Hi everyone!

I have an array which stores a regular expression as a string. Example:

[code]var myVar = new Array(“^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$”);
var thisRegExp = new RegExp(myVar[0]);
[/code]

I continuously get this error:

[code]invalid quantifier[/code]

However, If i do this:

[code]var thisRegExp = /^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$/;
if(thisRegExp.test(the_value)){..[/code]

Everything works fine. How can I get this to work?

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@thechasboiJul 20.2007 — try this
[CODE]
var myVar = new Array("^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$");
var newmyvar = myVar[0];
var thisRegExp = new RegExp(newmyvar );

[/CODE]
Copy linkTweet thisAlerts:
@bdichiaraauthorJul 20.2007 — I tried that. I still get the same error message.
Copy linkTweet thisAlerts:
@thechasboiJul 20.2007 — It seemes like your array is missing something from the regexp.

[CODE]
var myVar = new Array("[U]/[/U]^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$[U]/[/U]");
var newmyvar = myVar[0];
var thisRegExp = new RegExp(newmyvar );
[/CODE]
Copy linkTweet thisAlerts:
@bdichiaraauthorJul 20.2007 — when I add that, it fails everytime, no matter what the value of the field is.
Copy linkTweet thisAlerts:
@Banana_AnandaJul 20.2007 — You have to backslash all the backslashes, else they'll disappear. Look:

alert("^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$");



Why do you need to store them as strings ? Can't you just have an array of RegExps ?
Copy linkTweet thisAlerts:
@thechasboiJul 20.2007 — try this then
[CODE]
var myVar = new Array("/^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$/");
var newmyvar = new RegExp(myVar[0], 'g');
if(newmyvar.test(the_value)){..
[/CODE]
Copy linkTweet thisAlerts:
@Banana_AnandaJul 20.2007 — If you had tested that yourself, you'd know it doesn't work.

The forward slashes aren't included in the string when using the constructor, or they will be considered part of the expression.

alert( new RegExp ("/a/").toString() )
Copy linkTweet thisAlerts:
@bdichiaraauthorJul 20.2007 — The error message I get now is slightly different.

Before:

invalid quantifier ?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$

Now:

Before:

invalid quantifier ?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$/
Copy linkTweet thisAlerts:
@thechasboiJul 20.2007 — bdichiara

It seems that Banana Ananda gave me an idea. try putting .toString() at the end of the myVar[0] like this myVar[0].toString() this may force the array return to be a string and therefore give you a correctly formed regex I am grasping alittle cause I never done anything like this before. I assumed your regex worked by the way but assuming makes an ... you know the rest. ok hope this helps.
Copy linkTweet thisAlerts:
@Banana_AnandaJul 20.2007 — Start again:

The backslashes that you put into the string will be ignored because they will be considered escape sequences for the string (ones that have no effect). Escape them.

"^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$"

[B]Don't[/B] include the /.../. That denotes a regular expression literal.

Again. Why do you need to store them as strings ? You coud store them as RE literals, and save all this bother.
Copy linkTweet thisAlerts:
@bdichiaraauthorJul 20.2007 — I'm creating a script where I would like to be able to add more validators easily by just adding them to my array. I want to store them in their most natural format so it's easier to add more.

Is there a way (by means of a function) to take a standard RegExp (in a string) and escape it so it works in new RegExp?
Copy linkTweet thisAlerts:
@thechasboiJul 20.2007 — This may be reaching again but a suggestion no matter
[CODE]
var myVar = new Array("/^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$/");
var newmyvar = new RegExp('' + myVar[0] + '', 'g');
if(newmyvar.test(the_value)){..
[/CODE]

I have run across several occasions where I have had to insert the var into ' delimiters to make it act like a string for certain. Sorry for not being definate but just a reaching suggestion.
Copy linkTweet thisAlerts:
@Banana_AnandaJul 20.2007 — I would like to be able to add more validators easily by just adding them to my array. I want to store them in their most natural format so it's easier to add more[/QUOTE]

What would be more natural than storing them as a array of RegExps ?

var validators = [
/^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$/,
/^cat|dog$/,
/.{5}voo(doo)?/
];
Copy linkTweet thisAlerts:
@bdichiaraauthorJul 20.2007 — that's what I was just about to post. I tried storing it as:

usphone: {
className: "usphone",
defaultMessage: "This field must contain a valid US phone number.",
regExp: /^((?[0-9]{3}[)-. ]? ?)?[0-9]{3}[-. ]?[0-9]{4}$/
},


And even though it looks scary as part of the object (colors looks like it's going to break it), it appears to be working.

Thanks for the help!
×

Success!

Help @bdichiara spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...