473,806 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(f ormField,limit)
{
if (formField.matc h(/^\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 19378
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(f ormField,limit)
{
if (formField.matc h(/^\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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
ga****@galvao.e ti.br (Er Galv?o Abbott) writes:
if (formField.matc h(/^\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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
"Michael Winter" <M.******@bluey onder.co.invali d> 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*****@litote s.demon.co.uk> wrote:
"Michael Winter" <M.******@bluey onder.co.invali d> 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.******@blueyo nder.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
3178
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 "s": So when you look at this url: "http://www.goldenretrieverforum.com/search.php?s=5817617a59fb630a7f40846e4a29efc1&do=getdaily"
4
2668
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 existing PHP scripts of mine to work on IIS), I have created the following simple function to compare a Website visitor's IP address against a varabe-array. The experiment invovleas a common scenario -- banning a Website visitor by IP Address: ...
6
1776
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") %>',"(\d{3})(\d{3})(\d{4})","($1) $2-$3") runat="server"/> I have this inside my Repeater and want it to filter the field during bind. I can do it afterwards by just looping through the repeater items, but that is extra work and time.
4
3078
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 string ? A simple example will help. Thanks. ra294@hotmail.com
9
8890
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 and commas, and can use quotes to encapsulate multiple words. An example: tag1, tag2 tag3, "tag4 tag4, tag4" tag5, "tag6 tag6" So, as we can see here anything is allowed, but the problem is that splitting on commas obviously destroys tag4...
6
1741
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 these <brtags with \n\r. The replacement must be performed only within <preblocks. I can do this using VBScript in the following way: Option Explicit
6
1890
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. Is it possible?
10
3018
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 the text and clicking a standard "submit" button. MAIN PROBLEM: My problem is that the information transmitted to the formhandler apparently has some sort of whitespace added to it. If I simply use trim() on the POST variable, it fails the...
1
3887
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
10618
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.