简体   繁体   中英

Javascript: String.Replace

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

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');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM