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

How to use a variable name in a regex?

Hi, if I have a string assigned to a variable, how can I use that
variable in a regex?

e.g. I would like to do something like this:
var words = 'the quick brown browning fox';
var key = getKey();
var patt=/\bkey\b/;
patt.test(words);

Unfortunately I can't just put the string I'm searching for directly
in the regex, because it will be changing each time this bit of the
script is run.

Any help would be appreciated.
Cheers.
Oct 14 '08 #1
2 2850
Yansky wrote:
Hi, if I have a string assigned to a variable, how can I use that
variable in a regex?

e.g. I would like to do something like this:
var words = 'the quick brown browning fox';
var key = getKey();
var patt=/\bkey\b/;
Don't use a regular expression literal then, instead use the new RegExp
constructor
var patt = new RegExp("\\b" + key + "\\b");

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 14 '08 #2
On Oct 15, 3:38*am, Martin Honnen <mahotr...@yahoo.dewrote:
Yansky wrote:
Hi, if I have a string assigned to a variable, how can I use that
variable in a regex?
e.g. I would like to do something like this:
var words = 'the quick brown browning fox';
var key = getKey();
var patt=/\bkey\b/;

Don't use a regular expression literal then, instead use the new RegExp
constructor
* *var patt = new RegExp("\\b" + key + "\\b");

--

* * * * Martin Honnen
* * * *http://JavaScript.FAQTs.com/
Thanks, I had tried new RegExp earlier but I forgot to escape the
backslashes.
Cheers.
Oct 14 '08 #3

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 ...
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...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
1
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this...
0
by: Sebosac | last post by:
hi, novice on regex, i'm searching for THE master Regex will retieve php variable name like "$varname" in my script. $tagparse = fil_gzet_contents('myscript.php'); preg_match_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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.