Thread: replace()
View Single Post
 
Old January 16th, 2004, 11:17 AM
joefawcett's Avatar
joefawcett joefawcett is offline
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It might help if I could see the data passed as htmlIMG but I think you need:
Code:
var oRE = new RegExp(tempImg, "i");
htmlIMG.replace(oRE, bioView)
String.protoype.replace needs a regular expression as its first argument and a string as its second. It also doesn't replace inline, it trerns a new String with the new text. Regular expressions can be created quickly using the forward slash delimiters, /, but these take a string literal. You are using a variable, tempIMG so you need the two stage creation shown above. The i flag indicates case insensitive. The main problem is that if your target string contains special characters, especially back slashes, \, you need to double these up before creating the Regular Expression.
As you know where the string starts and ends it might be easier just using String methods. Concatenate the string from htmlIMG upto the start of the a tag, the new text (bioView), and add on the text after the end of the a tag.
If you show me an example of possible inputs and desired outputs perhaps I can come up with a better explanation.



--

Joe