How to build a regular expression on runtime?

H

Harry

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
 
M

Michael Winter

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
 
A

Alberto

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/
 
A

Alberto

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
 
H

Harry

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
 
E

Evertjan.

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")
if (somestring.search(re)>=0)
 
T

Thomas 'PointedEars' Lahn

Harry said:
[...] 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).test(somestring))
{
...
}

You should be aware that Regular Expressions may contain special characters
(".", "?" etc.) You must escape them in somestringarray 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.
 
A

Alberto

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, "gi");
if( somestring.match( regExpObject ))
{alert(somestringarray)
break;
}
}
//--></script>

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

Alberto

You're very welcome. Happy tho have been helpful.

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


Harry said:
Hi,

Ok, I´ve it now. Thank you all for the helpful replies. Great forum!

Harry



"Alberto" <[email protected]> wrote in message
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, "gi");
if( somestring.match( regExpObject ))
{alert(somestringarray)
break;
}
}
//--></script>

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top