Replacement Text Syntax for JavaScript’s String.replace()
Posted
by Jan Goyvaerts
on Regular-Expressions.info
See other posts from Regular-Expressions.info
or by Jan Goyvaerts
Published on Tue, 08 Jun 2010 10:16:09 +0000
Indexed on
2010/06/08
10:23 UTC
Read the original article
Hit count: 769
Regex Libraries
A RegexBuddy user told me that he couldn’t easily find a detailed explanation of the replacement text syntax supported by the String.replace()
function in JavaScript. I had to admin that my own web page about JavaScript’s regular expression support was also lacking. I’ve now added a new Replacement Syntax section that has all the details. I’ll summarize it here:
$1
: Text matched by the first capturing group or the literal text $1 if the regex has no capturing groups.$99
: Text matched by the 99th capturing group if the regex has 99 or more groups. Text matched by the 9th capturing group followed by a literal 9 if the regex has 9 or more but less than 99 groups. The literal text $99 if the regex has fewer than 9 groups.$+
: Text matched by the highest-numbered capturing group. Replaced with nothing if the highest-numbered group didn’t participate in the match.$&
: Text matched by the entire regex. You cannot use$0
for this.$`
(backtick): Text to the left of the regex match.$'
(single quote): Text to the right of the regex match.$_
: The entire subject string.
© Regular-Expressions.info or respective owner