Replace method using javascript

I will get the data from the server as NY^NJ^CA^MI or NC^SC etc and need to replace “^” with “,” (coma) . I have tried following two approaches but not working.
Can anyone guide me what went wrong with these approaches?

Ex: var str=“NY^NJ^CA^MI”;

var test = str.replace(/^/g, “,”);

or

var test = str.replace(/^/g, ‘,’);

Thanks in advance

Hi,

Good question.

The problem, is that the caret (^) is a special character in regular expressions which normally represents the start of the current line.
You will need to escape it if you want to match a normal caret:

var test = str.replace(/\\^/g, ","); 

Thank you so much.It worked