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" <nospam@nospam.nospam> wrote in message news:<Hr8Uc.145776$5D1.7197081@news4.tin.it>...[color=blue]
> 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" <nospam@nospam.nospam> ha scritto nel messaggio
> news:Jn8Uc.145762$5D1.7196307@news4.tin.it...[color=green]
> > 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[/color]
> GLOBAL[color=green]
> > scope (search ALL the occurrencies, that is), i stands for case[/color]
> INsensitive[color=green]
> > 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[/color]
> with[color=green]
> > HI*/
> > alert(test_string)
> > //--></script>
> >
> > For an overview of the javascript methods that deal with regular[/color]
> expressions[color=green]
> > (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" <harald.humml@t-systems.at> ha scritto nel messaggio
> > news:b3271585.0408160815.4ef7c45@posting.google.co m...[color=darkred]
> > > 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[/color]
> >
> >[/color][/color]