3

I'm trying to replace a special character (^) in javascript with string.replace but am not having much luck.

Here's what I've tried so far:

var Temp;
Temp = lstRsns.options(i).text;

Temp = Temp.replace(/^/g, '\r\n');
Temp = Temp.replace(/'^'/g, '\r\n');
Temp = Temp.replace(/"^"/g, '\r\n');
Temp = Temp.replace(/^/g, "\r\n");
Temp = Temp.replace(/'^'/g, "\r\n");
Temp = Temp.replace(/"^"/g, "\r\n");

Any ideas? I get the text value from my listbox alright, it's just the darned ^ won't go away.

Thanks in advance for any/all help.

-Jason

1
  • Did you look at this thread? Commented Aug 11, 2011 at 16:41

2 Answers 2

8

It's a regexp meta character, and therefore needs to be escaped so it is treated as a literal:

Temp = Temp.replace(/\^/g, '\r\n');
Sign up to request clarification or add additional context in comments.

Comments

1

you need to escape it with \ because it's a special character

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.