469,927 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Variable inside RegEx

Greetings.

I have a function that does some pattern matching with JS's RegEx and
I'm trying to use a variable inside of it. Nothing that I've done
worked, so please help me.

Here is the func:

function validateField(formField,limit)
{
if (formField.match(/^\d{1,11}\,\d{2}$/))
{
return true;
}
}

What I need is to replace the "11" in the RegEx with the var limit.

How can I do this???

TIA,
Jul 20 '05 #1
4 19124
On 16 Feb 2004 09:19:00 -0800, Er Galv?o Abbott <ga****@galvao.eti.br>
wrote:
Greetings.

I have a function that does some pattern matching with JS's RegEx and
I'm trying to use a variable inside of it. Nothing that I've done
worked, so please help me.

Here is the func:

function validateField(formField,limit)
{
if (formField.match(/^\d{1,11}\,\d{2}$/))
{
return true;
}
}

What I need is to replace the "11" in the RegEx with the var limit.

How can I do this???


As you've no doubt discovered, literals cannot have variables in them.
However, the RegExp() constructor can:

function validateField( formField, limit ) {
var re = new RegExp( '^\d{1,' + limit + '}\,\d{2}$' );

return formField.match( re );
}

If you need to use flags, add them as a string as the second parameter.
Global, for example:

... = new RegExp( 'pattern', 'g' );

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
ga****@galvao.eti.br (Er Galv?o Abbott) writes:
if (formField.match(/^\d{1,11}\,\d{2}$/))
(no need to escape the comma)
What I need is to replace the "11" in the RegEx with the var limit.


If the regular expression isn't constant, you can't use the literal
notation, and must construct the regular expression using the RegExp
function instead.

RegExp("^\\d{1," + limit + "},\\d{2}$")

Notice that the argument to RegExp is a string literal, and as such
treats backslashes special. To include a backslash in the generated
regular expression, you must write two in the string literal.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
function validateField( formField, limit ) {
var re = new RegExp( '^\d{1,' + limit + '}\,\d{2}$' );

<snip>

But is necessary to escape the escape characters in the string literals
provided for the RegExp constructor, something like (untested):-

var re = new RegExp( '^\\d{1,' + limit + '}\\,\\d{2}$' );

Richard.
Jul 20 '05 #4
On Mon, 16 Feb 2004 18:39:59 -0000, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
function validateField( formField, limit ) {
var re = new RegExp( '^\d{1,' + limit + '}\,\d{2}$' );

<snip>

But is necessary to escape the escape characters in the string literals
provided for the RegExp constructor, something like (untested):-


[snipped example]

*Slaps forehead*

Quite right.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jozef Jarosciak | last post: by
6 posts views Thread by tshad | last post: by
10 posts views Thread by Mason Barge | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.