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.
The following code uses the replace method to alter 'DevGuru.com'
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.
myString = new String("Go to DevGuru.com")
rExp = /devguru.com/gi;
newString = new String ("http://www.devguru.com")
results = myString.replace(rExp, newString.link("http://www.devguru.com"))
Go to http://www.devguru.com