473,395 Members | 1,484 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,395 software developers and data experts.

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

Similar topics

4
by: Jozef Jarosciak | last post by:
Hi everyone, I am building a web crawler and one of the features which I need to include is exclusion of specified 'variable + value' from the url. Example, user wanted to extract variable...
4
by: Friday | last post by:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of dot.net (and all things Windows, for that matter). As part of an experiment (to learn enough ASP/VB.net to port a series of ...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
4
by: Ya Ya | last post by:
Hi, I have a string with some fixed text and variable text. For example: "this is a fixed text THE NEEDED INFO more more fixed text". How do I get the the variable text (THE NEEDED INFO) from this...
9
by: conspireagainst | last post by:
I'm having quite a time with this particular problem: I have users that enter tag words as form input, let's say for a photo or a topic of discussion. They are allowed to delimit tags with spaces...
6
by: alainfri | last post by:
I am not sure if this group is the right place for this question but what I need is as follows. There is a piece of html. Throughout the html there are a lot of <brtags. The task is to replace all...
6
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that....
10
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying...
1
by: faultykid | last post by:
I would like to store a variable then call it back later. I have a variable on line 198 www = ''+this._ad.clickUrl+''; and on line 321 i try document.write(www);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.