Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Carrot Character

Status
Not open for further replies.

shift314

Programmer
Jun 3, 2007
1
US
I'm writing a javascript calculator and need to replace the '^' character. Javascript does not seem to recognize this character correctly in a string.

var test="abc^d";
var searchstring="c"
alert(test.search(searchstring));

The above code correctly displays 2 when I change the c to ^, I get 0, not even -1. I get zero regardless of the position of ^. If I search for "c^" I get -1.

Any help is appreciated.
 
You need to escape the ^, as it has a special meaning in regular expressions (which is what we're dealing with here in the search method).

If you use:

Code:
var searchstring = "\\^";

then all works as expected.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top