views:

86

answers:

1

why does the following js expression:

"test1 foo bar test2".replace(/foo.bar/, "$'")

result in the following string?

"test1  test2 test2"

is the $' in the replace string some sort of control code for including everything after the match???

this behavior was screwing with me most of the day. can anyone explain this?

thanks a lot

ps- this is the case in all browsers i've tested

+5  A: 

In a regex replace parameter, you need to escape the $:

"test1 foo bar test2".replace(/foo.bar/, "$$'")

$' inserts the portion of the string that follows the matched substring.
See the documentation.

SLaks
right, cool. i knew u needed to escape dollar signs (for cases like $0 or $1) but didnt know what the $' was for. thanks for the link
arshaw