Quick References
ADO
ASP
CSS2
HTML
JavaScript
Jet SQL
VBScript
WML
WMLScript
WSH
XHTML
XML DOM
XSLT
Features
Knowledge Base
Tutorials
Partners
ZVON.ORG
XML
Planet Source Code
VisualBuilder
Web Design
Your HTML Source
XML/XSLT Forums
ASPAlliance
Scripts
Programmers Heaven
Tek-Tips Forums
Developer Fusion
Code Project
All Objects
String Object
METHOD: String::replace
object.
replace
(regexp, newSubStr)
object.
replace
(regexp, function)
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' 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.
Code:
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)
Output:
Go to
http://www.devguru.com
today!
Copyright 1999-2005 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information