/    Sign up×
Community /Pin to ProfileBookmark

Regular Expressions with variables

Hi folks-

I need to know if it’s possible to use javascript variables within a regular expression…..how would this work? If I had the following:

[CODE]//Validate DL for 11 digits or SSN or 8 digits
//South Dakota
function validate_dl_34(str_dlstate)
{
var ssn = new String(“getField( “SSN” )”)
var re = /^d{11}$|^d{8}$/;
return re.test(str_dlstate);
}[/CODE]

This will validate for 11 digits or 8 digits……as you see I have the SSN pulling from a database and stored in a new variable. At that point, how could I include that variable within the regular expression?

Driving me nuts, don’t see any references online about this……thanks!! ?

T

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Dec 03.2007 — Using your example:
<i>
</i>function validate_dl_34(str_dlstate)
{
var ssn = new String("getField( "SSN" )")
var re = /^(d{11})$|^(d{8})$/;
var result = ssn.match(re);

result[1] = //matched result
}
Copy linkTweet thisAlerts:
@SarasotaTimauthorDec 03.2007 — Ahhhh ok, so, this will then return true if the string is 11 digits OR 8 digits OR the SSN pulled from the db? I only ask because I don't see any references to the SSN field in the re variable, which is the actual regular expression.

ALSO.....will this work when using string functions? For example, some of these will validate for, example, first letter of last name and 14 digits...how do I string this expression together to say the var "str" (which is the first letter of last name) and 14 digits after?

[CODE]function validate_dl_32(str_dlstate)
{
var str = new String("getField( "LNAME" )")
str = str.charAt(0)
var re = /^d{14}$/;
return re.test(str_dlstate);
}[/CODE]


Using your example:
<i>
</i>function validate_dl_34(str_dlstate)
{
var ssn = new String("getField( "SSN" )")
var re = /^(d{11})$|^(d{8})$/;
var result = ssn.match(re);

result[1] = //matched result
}
[/QUOTE]
Copy linkTweet thisAlerts:
@TJ111Dec 03.2007 — First of all, while I'm thinking of this, I believe a better syntax would be:
<i>
</i>var re = /^(d{8}|d{11})$/;

And there doesn't need to be a reference to the SSN in the re variable. You are comparing the string "ssn" to the pattern defined in "re", if there is a match, result will contain an array with the matched results (even if there is only one match, it will still be an array, because there are other indexes for the array containing extra information). If there isn't a match, result would return "null", not false.

Here is a great [url=http://www.regular-expressions.info]regular expression reference[/url] I keep bookmarked, great resource for writing regular expressions.
Copy linkTweet thisAlerts:
@TJ111Dec 03.2007 — To your second question, regular expressions can be defined for any situation. For example, to get the first letter of a last name followed by 14 numbers, it would be (and containing ONLY 1 letter and 14 numbers:
<i>
</i>var re = /^[A-Z]d{14}$/i; //i flag means case insensitive
Copy linkTweet thisAlerts:
@SarasotaTimauthorDec 03.2007 — I'm sorry, I don't understand.....the code below just validates that it's ANY letter and 14 numbers, right? It doesn't validate anything about the FIRST letter of a given string?

To your second question, regular expressions can be defined for any situation. For example, to get the first letter of a last name followed by 14 numbers, it would be (and containing ONLY 1 letter and 14 numbers:
<i>
</i>var re = /^[A-Z]d{14}$/i; //i flag means case insensitive
[/QUOTE]
Copy linkTweet thisAlerts:
@TJ111Dec 03.2007 — Go to the link I posted and read through the tutorial. It'll take a little over 20 minutes, but will really make you comprehend regular expressions to their fullest.

If you wanted to validate for the first letter ebing a specific letter, theres a few options you could use -- multiple regexps, dynamically generated regexps, etc. Depends on how the first letter is created and what your validating it for/against.
×

Success!

Help @SarasotaTim 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.16,
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,
)...