From MDN
The
search()
method executes a search for a match between a regular expression and thisString
object.
str.search(regexp)
So it expects a regex. ^
is a regex special character. You need to escape it:
n = string.search("\\^");
Or simply use a regex:
n = string.search(/\^/);