...

View Full Version : Insert variable into regular expression



jmh722
07-10-2005, 07:03 PM
I am trying to turn selected text into bold text. In order to do that, I need to insert them into an array.
How do I insert the variables from the Array into the variable containig the regular expression?



jan= new String("The traditional birthstone for February is Amethyst");
expArray = new Array('Amethyst','The');
for (i = 0; i < expArray.length -1; i++){
regExp = /"+expArray[i]+"{1}/;
newString = new String('<b>'+ expArray[i]+'</b>');
result = jan.replace(regExp, newString);
document.write(result);
}

enumerator
07-10-2005, 08:52 PM
Syntax 1

re = /pattern/[flags]

Syntax 2

re = new RegExp("pattern",["flags"])


syntax2...

jmh722
07-10-2005, 08:59 PM
I'm sorry, I don't quite get it. Could you explain what you wrote up there?

enumerator
07-10-2005, 09:08 PM
This covers it: http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjregexpression.asp

jmh722
07-10-2005, 09:16 PM
I got this code to Bold selected text, but now it returns the text twice in a row. Like this:

The traditional birthstone for February is
AmethystThe traditional birthstone for February is
Amethyst

I'm really new to regular expressions, what should I do?





jan= new String("The traditional birthstone for February is Amethyst");
expArray = new Array('Amethyst','The');
for (i = 0; i < expArray.length; i++){
regExp=eval('/'+expArray[i]+'{1}/');
newString = new String('<b>'+ expArray[i]+'</b>');
result = jan.replace(regExp, newString);
document.write(result);
}

Amethyst

enumerator
07-10-2005, 09:19 PM
First read the manual... (some of it, at least). :)

jscheuer1
07-10-2005, 09:37 PM
<script type="text/javascript">
jan= new String("The traditional birthstone for February is Amethyst");
expArray = new Array('Amethyst','The');
for (i = 0; i < expArray.length; i++){
regExp=eval('/'+expArray[i]+'{1}/');
newString = new String('<b>'+ expArray[i]+'</b>');
jan = jan.replace(regExp, newString);
}
document.write(jan);
</script>

jmh722
07-10-2005, 09:54 PM
I am honestly reading the manual, and still having pronblems.
As far as I can tell, the replace() thinks that it has to write a new line of text for every instance of [i]. SO I guess what I need to figure out is how to make it parse all bold text into a single line.

jmh722
07-10-2005, 09:59 PM
Nevermind. Figured it out.
Thanks one and all!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum