The below code gives the error:
var a = "hello {0} world"; a.replace(/{0}/g, "little");
But this one works:
var a = "hello {str} world"; a.replace(/{str}/g, "little");
Why do we get this error?
Because {n} (with n being a positive integer) is quantifier syntax, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers
{n}
n
You don’t want the curly braces to have their “special meaning” here, but want them treated as plain characters - so you should escape them.
a.replace(/{0}/g,"little")
1.4m articles
1.4m replys
5 comments
56.9k users