Q:

escaping $ sign for String.replace

At following String.replace line, lossLocation.state$ ,
the $ sign is removed after the replace,
i need to keep the $, as it is used in the variable.
 '{0}'.replace(
         '{0}'
       , 'categories.Country === \'$formData.lossLocation.state$\'.toUpperCase()')

It gives me
"categories.Country === '$formData.lossLocation.state'.toUpperCase()"

The expected outcome should be
"categories.Country === '$formData.lossLocation.state$'.toUpperCase()"

i've tried the following but still been removed when replace
state\$

A:

As it states in String.prototype.replace(). To escape '$' in replacement content, you should use '$$' instead of '\$'.
So a proper way of constructing it would be
'{0}'.replace('{0}', 
    'categories.Country === \'$formData.lossLocation.state$\'.toUpperCase()'
        .replace(/\$/g, '$$$$')
)

