Replacing carets in Javascript
javascript
Solution
The `^` character matches the start of the string in a regex, so you need to escape it so it will be treated as a literal character. That is why in your example, the result string has a new space added at the start of the string.
var result = str.replace(/\^/g, " ");
Problem
I'm trying to replace multiple carets (^) in a string with spaces in Javascript. Following the w3schools entry on replace(), I used this code: ``` var str = "Salt^Lake^City, UT"; var result = str.replace(/^/g, " "); ``` However, value of `result` is " Salt^Lake^City, UT". One caret is replaced when I run this code: ``` var result = str.replace("^", " "); ``` but I want to replace all of an arbitrary number of carets. Is there something obvious I'm missing about globally replacing in Javascript? I could write a function using `str.replace("^", " ");` to remove all the carets, but I'd rather use the built-in global replace.