2

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.