String.prototype.replace supports replacement patterns (#tilPost)

Today I came across a blog post (it’s in German though) written by Peter Kröner and learned something very astonishing.
The article describes the not so well known behaviors of the method String.prototype.replace. Using this method …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Today I came across a blog post (it's in German though) written by Peter Kröner and learned something very astonishing.

The article describes the not so well known behaviors of the method String.prototype.replace. Using this method is usually very straight forward. It supports regular expressions if you need to, but for most cases, it's defining a matching string and a replacing string.

That's at least what I thought... ?

It turns out that if the second argument is a string (it can also be a function) and includes specific character sequences like $& or $' "replacer magic" appears.

const msg = 'This is a great message';

msg.replace('great', 'wonderful'); 
// "This is a wonderful message"
//
// -> 'great' is replaced by 'wonderful'

msg.replace('great', '$&-$&');
// "This is a great-great message"
// '$&' represents the matched substring
//
// -> 'great' is replaced by 'great-great'

msg.replace('great', '$`');
// "This is a This is a  message"
// '$`' represents the string before the matched string
//
// -> 'great' is replaced by 'This is a '

msg.replace('great', `$'`)
// "This is a  message message"
// `$'` represents the string after the matched string
//
// -> 'great' is replaced by ' message'

Oh my..., this behavior can lead to very hard to spot bugs!

If you want to read more about it, have a look at the replace docs on MDN or this overview on replacement values.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2023-02-04T23:33:20+00:00) » String.prototype.replace supports replacement patterns (#tilPost). Retrieved from https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/.
MLA
" » String.prototype.replace supports replacement patterns (#tilPost)." Stefan Judis | Sciencx - Tuesday July 30, 2019, https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/
HARVARD
Stefan Judis | Sciencx Tuesday July 30, 2019 » String.prototype.replace supports replacement patterns (#tilPost)., viewed 2023-02-04T23:33:20+00:00,<https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » String.prototype.replace supports replacement patterns (#tilPost). [Internet]. [Accessed 2023-02-04T23:33:20+00:00]. Available from: https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/
CHICAGO
" » String.prototype.replace supports replacement patterns (#tilPost)." Stefan Judis | Sciencx - Accessed 2023-02-04T23:33:20+00:00. https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/
IEEE
" » String.prototype.replace supports replacement patterns (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/. [Accessed: 2023-02-04T23:33:20+00:00]
rf:citation
» String.prototype.replace supports replacement patterns (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2019/07/30/string-prototype-replace-supports-replacement-patterns-tilpost/ | 2023-02-04T23:33:20+00:00
https://github.com/addpipe/simple-recorderjs-demo