By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

How to build a regular expression on runtime?

P: n/a
Hi there,

does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables? Should be something like this:

var strkey = "something";
var str = "Somethin like this";

if( str.search( / + strkey + / ) > -1 )
{
...
}

TIA
Harry
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 16 Aug 2004 09:15:34 -0700, Harry <ha**********@t-systems.at> wrote:
does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables?


[snip]

Use the regular expression constructor to create an object:

var re = new RegExp(pattern, flags);
var str = str.replace(re, replacement);

Both pattern and flags are strings (flags is optional), so remember to
escape characters properly. For example,

/\d/ becomes '\\d'

Notice that the forward slashes aren't present in the pattern.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #2

P: n/a
Proceed as follows (one suggestion out of many of course):

<script><!--
var test_string="hallo guy hallo! I said hallo!";
var yourRegExp_as_text="\\w{5}"; //that checks for 5 letters long words
var regExpObject=new RegExp();
//now use the built in method compile
regExpObject.compile(yourRegExp_as_text, "gi");
/*the second argument takes in the so called modifiers. g stands fopr GLOBAL
scope (search ALL the occurrencies, that is), i stands for case INsensitive
search*/

//Now you have a valid regular expression
//use it with methods that are regular expression methods, instance
replace():
test_string=test_string.replace(regExpObject, "HI"); /*replaced hallo with
HI*/
alert(test_string)
//--></script>

For an overview of the javascript methods that deal with regular expressions
(NOT the best summary available online of course! just one among many
others!):
http://www.unitedscripters.com/spellbinder/regexp.html

I hope this helps
ciao
Alberto
http://www.unitedscripters.com/


"Harry" <ha**********@t-systems.at> ha scritto nel messaggio
news:b3*************************@posting.google.co m...
Hi there,

does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables? Should be something like this:

var strkey = "something";
var str = "Somethin like this";

if( str.search( / + strkey + / ) > -1 )
{
...
}

TIA
Harry

Jul 23 '05 #3

P: n/a
PS if the line

//use it with methods that are regular expression methods, instance
replace():

gets broken like it does on my reader, eliminate that ISOLATED replace():
that was not code but a part of a comment

ciao
Alberto

"Alberto" <no****@nospam.nospam> ha scritto nel messaggio
news:Jn**********************@news4.tin.it...
Proceed as follows (one suggestion out of many of course):

<script><!--
var test_string="hallo guy hallo! I said hallo!";
var yourRegExp_as_text="\\w{5}"; //that checks for 5 letters long words
var regExpObject=new RegExp();
//now use the built in method compile
regExpObject.compile(yourRegExp_as_text, "gi");
/*the second argument takes in the so called modifiers. g stands fopr GLOBAL scope (search ALL the occurrencies, that is), i stands for case INsensitive search*/

//Now you have a valid regular expression
//use it with methods that are regular expression methods, instance
replace():
test_string=test_string.replace(regExpObject, "HI"); /*replaced hallo with HI*/
alert(test_string)
//--></script>

For an overview of the javascript methods that deal with regular expressions (NOT the best summary available online of course! just one among many
others!):
http://www.unitedscripters.com/spellbinder/regexp.html

I hope this helps
ciao
Alberto
http://www.unitedscripters.com/


"Harry" <ha**********@t-systems.at> ha scritto nel messaggio
news:b3*************************@posting.google.co m...
Hi there,

does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables? Should be something like this:

var strkey = "something";
var str = "Somethin like this";

if( str.search( / + strkey + / ) > -1 )
{
...
}

TIA
Harry


Jul 23 '05 #4

P: n/a
Alberto,
Michael,

Thank you for your replies. The Problem is not to replace a string
with regexp. The Problem is to build a search-regexp dynamically at
runtime. Naturally I will search with regexp like this:

var somestring = "somecontent";
if( somestring.search( /some/ ) > -1 )
{
...
}

In this example, the problem is that the searchstring is hardcoded.
What I want is:

var somestringarray = new Array( "one", "two", "three" );
var somestring = "This is one of my favorite";
for( var i = 0; i < somestringarray.length; i++ )
{
if( somestring.search( somestringarray[ i ] ) > -1 )
{
...
break;
}
}

How can I achieve this?

TIA
Harry

"Alberto" <no****@nospam.nospam> wrote in message news:<Hr**********************@news4.tin.it>...
PS if the line

//use it with methods that are regular expression methods, instance
replace():

gets broken like it does on my reader, eliminate that ISOLATED replace():
that was not code but a part of a comment

ciao
Alberto

"Alberto" <no****@nospam.nospam> ha scritto nel messaggio
news:Jn**********************@news4.tin.it...
Proceed as follows (one suggestion out of many of course):

<script><!--
var test_string="hallo guy hallo! I said hallo!";
var yourRegExp_as_text="\\w{5}"; //that checks for 5 letters long words
var regExpObject=new RegExp();
//now use the built in method compile
regExpObject.compile(yourRegExp_as_text, "gi");
/*the second argument takes in the so called modifiers. g stands fopr

GLOBAL
scope (search ALL the occurrencies, that is), i stands for case

INsensitive
search*/

//Now you have a valid regular expression
//use it with methods that are regular expression methods, instance
replace():
test_string=test_string.replace(regExpObject, "HI"); /*replaced hallo

with
HI*/
alert(test_string)
//--></script>

For an overview of the javascript methods that deal with regular

expressions
(NOT the best summary available online of course! just one among many
others!):
http://www.unitedscripters.com/spellbinder/regexp.html

I hope this helps
ciao
Alberto
http://www.unitedscripters.com/


"Harry" <ha**********@t-systems.at> ha scritto nel messaggio
news:b3*************************@posting.google.co m...
Hi there,

does anyone know how I can build a regular expression e.g. for the
string.search() function on runtime, depending on the content of
variables? Should be something like this:

var strkey = "something";
var str = "Somethin like this";

if( str.search( / + strkey + / ) > -1 )
{
...
}

TIA
Harry


Jul 23 '05 #5

P: n/a
Harry wrote on 17 aug 2004 in comp.lang.javascript:
In this example, the problem is that the searchstring is hardcoded.
What I want is:

var somestringarray = new Array( "one", "two", "three" );
var somestring = "This is one of my favorite";
for( var i = 0; i < somestringarray.length; i++ )
{
if( somestring.search( somestringarray[ i ] ) > -1 )
{
...
break;
}


re = new RegExp(somestringarray[i],"i")
if (somestring.search(re)>=0)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #6

P: n/a
Harry wrote:
[...] The Problem is to build a search-regexp dynamically at
runtime. Naturally I will search with regexp like this:

var somestring = "somecontent";
if( somestring.search( /some/ ) > -1 )
{
...
}

In this example, the problem is that the searchstring is hardcoded.
What I want is:

var somestringarray = new Array( "one", "two", "three" );
var somestring = "This is one of my favorite";
for( var i = 0; i < somestringarray.length; i++ )
{
if( somestring.search( somestringarray[ i ] ) > -1 )
{
...
break;
}
}

How can I achieve this?
if (new RegExp(somestringarray[i]).test(somestring))
{
...
}

You should be aware that Regular Expressions may contain special characters
(".", "?" etc.) You must escape them in somestringarray[i] prior if they
should not have their special meaning, i.e. you must work on a ruleset of
RegExps allowed for your users. If no RegExp at all is allowed for input,
use the escapeRE() method I posted a few days before.

And if you do not search for patterns, but only for strings, you should use
String.prototype.indexOf() instead, not requiring any RegExp object, which
is usually faster.
[Top post]


Please don't, see the <http://jibbering.com/faq/>.
PointedEars

P.S.: de.comp.lang.javascript exists.
--
Let's introduce General Protection to Corporal Punishment
Jul 23 '05 #7

P: n/a
Hello

it doesn't matter whether you want to replace or search: what you basically
need is to compile a regular expression on the run, ISN'T IT?
Try the code I sent to you:

<script><!--
var test_string="hallo guy hallo! I said hallo!";
var yourRegExp_as_text="\\w{5}"; //that checks for 5 letters long words
var regExpObject=new RegExp();
//now use the built in method compile
regExpObject.compile(yourRegExp_as_text, "gi");
/*the second argument takes in the so called modifiers. g stands fopr GLOBAL
scope (search ALL the occurrencies, that is), i stands for case INsensitive
search*/

//Now you have a valid regular expression
/*use it with methods that are regular expression methods, instance
replace():*/
test_string=test_string.replace(regExpObject, "HI"); /*replaced hallo with
HI*/
alert(test_string)
//--></script>

Once you don't need replacing, that's not a problem, you can use search:
test_string.seRCH(regExpObject);

You'd have to work a bit yourself on the codes sent as a reply.
Anyway, to get closer to your needs:

<script><!--
var somestringarray = new Array( "one", "two", "three" );
var somestring = "This is one of my favorite";
var regExpObject=new RegExp();
for( var i = 0; i < somestringarray.length; i++ )
{
regExpObject.compile(somestringarray[i], "gi");
if( somestring.match( regExpObject ))
{alert(somestringarray[i])
break;
}
}
//--></script>

ciao
Alberto
http://www.unitedscripters.com/


Jul 23 '05 #8

P: n/a
Hi,

Ok, Ive it now. Thank you all for the helpful replies. Great forum!

Harry

"Alberto" <no****@nospam.nospam> wrote in message news:<Fm**********************@news4.tin.it>...
Hello

it doesn't matter whether you want to replace or search: what you basically
need is to compile a regular expression on the run, ISN'T IT?
Try the code I sent to you:

<script><!--
var test_string="hallo guy hallo! I said hallo!";
var yourRegExp_as_text="\\w{5}"; //that checks for 5 letters long words
var regExpObject=new RegExp();
//now use the built in method compile
regExpObject.compile(yourRegExp_as_text, "gi");
/*the second argument takes in the so called modifiers. g stands fopr GLOBAL
scope (search ALL the occurrencies, that is), i stands for case INsensitive
search*/

//Now you have a valid regular expression
/*use it with methods that are regular expression methods, instance
replace():*/
test_string=test_string.replace(regExpObject, "HI"); /*replaced hallo with
HI*/
alert(test_string)
//--></script>

Once you don't need replacing, that's not a problem, you can use search:
test_string.seRCH(regExpObject);

You'd have to work a bit yourself on the codes sent as a reply.
Anyway, to get closer to your needs:

<script><!--
var somestringarray = new Array( "one", "two", "three" );
var somestring = "This is one of my favorite";
var regExpObject=new RegExp();
for( var i = 0; i < somestringarray.length; i++ )
{
regExpObject.compile(somestringarray[i], "gi");
if( somestring.match( regExpObject ))
{alert(somestringarray[i])
break;
}
}
//--></script>

ciao
Alberto
http://www.unitedscripters.com/

Jul 23 '05 #9

P: n/a
You're very welcome. Happy tho have been helpful.

ciao
Alberto
http://www.unitedscripters.com/
"Harry" <ha**********@t-systems.at> ha scritto nel messaggio
news:b3**************************@posting.google.c om...
Hi,

Ok, Ive it now. Thank you all for the helpful replies. Great forum!

Harry

"Alberto" <no****@nospam.nospam> wrote in message

news:<Fm**********************@news4.tin.it>...
Hello

it doesn't matter whether you want to replace or search: what you basically need is to compile a regular expression on the run, ISN'T IT?
Try the code I sent to you:

<script><!--
var test_string="hallo guy hallo! I said hallo!";
var yourRegExp_as_text="\\w{5}"; //that checks for 5 letters long words
var regExpObject=new RegExp();
//now use the built in method compile
regExpObject.compile(yourRegExp_as_text, "gi");
/*the second argument takes in the so called modifiers. g stands fopr GLOBAL scope (search ALL the occurrencies, that is), i stands for case INsensitive search*/

//Now you have a valid regular expression
/*use it with methods that are regular expression methods, instance
replace():*/
test_string=test_string.replace(regExpObject, "HI"); /*replaced hallo with HI*/
alert(test_string)
//--></script>

Once you don't need replacing, that's not a problem, you can use search:
test_string.seRCH(regExpObject);

You'd have to work a bit yourself on the codes sent as a reply.
Anyway, to get closer to your needs:

<script><!--
var somestringarray = new Array( "one", "two", "three" );
var somestring = "This is one of my favorite";
var regExpObject=new RegExp();
for( var i = 0; i < somestringarray.length; i++ )
{
regExpObject.compile(somestringarray[i], "gi");
if( somestring.match( regExpObject ))
{alert(somestringarray[i])
break;
}
}
//--></script>

ciao
Alberto
http://www.unitedscripters.com/

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.