This method is used to match a specifed regular expression against a string and replace any match with a new substring.
This method is used to match a specifed regular expression
against a string and replace any match with a new substring.
The newSubStr> argument
can include certain RegExp properties. These are: $1
thru $9, lastMatch, lastParen, leftContext
and rightContext (for details on the RegExp
object's properties, go here).
To perform a global match include the 'g' (global)
flag in the regular expression and to perform a case-insensitive
match include the 'i' (ignore case) flag.
The second argument can also be a function which is invoked
after the match is performed and the result of which is used
as the replacement string.
myString = new String("Go to DevGuru today!")
rExp = /devguru/gi;
newString = new String ("http://www.devguru.com")
results = myString.replace(rExp, newString.link("http://www.devguru.com"))
document.write(results)Go to http://www.devguru.com today!This code uses the replace method to alter 'DevGuru' in the original string to the full URL for the DevGuru website and then uses the String.link method to provide a hyperlink to the site.