Reputation: 4404
I can't, for the life of me, figure out how to replace all [
with <
and all ]
with >
in a value in JavaScript.
Here's what I've tried and it just doesn't work:
function SwitchDialog(NextDialog)
{
if (NextDialog=='SCHEDULE') {
$('.popup1').smart_modal_hide();
} else if (NextDialog=='LICENSEE') {
gotoLicensee(licenseeLink);
} else if (NextDialog=='REVIEWCART') {
window.location = CART_URL;
} else if (NextDialog=='RFO') {
window.location = REGISTER_FOR_OTHERS_URL;
} else {
if (NextDialog=='PREREG') {
var msgtxt = gk_text.replace(//[/g, '<');
msgtxt = msgtxt.replace(//]/g, '>');
DialogTemps[NextDialog].msgtext = msgtxt; // error occurs on this line
}
RenderDialog(NextDialog);
}
}
I get a Syntax error: "missing ) after argument list
at the ; on the line that assigns the msgtext
.
What the heck am I doing wrong?
Upvotes: 0
Views: 437
Reputation: 43663
You need to escape [
and ]
characters with \
, as then have special meaning in regex.
var msgtxt = gk_text.replace(/\[/g, '<').replace(/\]/g, '>');
Upvotes: 0
Reputation: 48761
You commented out the closing )
, hence the error.
You need to escape a /
in a regex, and not a bad idea to escape the [
and ]
as well, even though it's likely not needed here.
var msgtxt = gk_text.replace(/\/\[/g, '<');
msgtxt = msgtxt.replace(/\/\]/g, '>');
Upvotes: 4
Reputation: 108
You need to escape the [ and ] characters in your expression with a backslash (\), like so:
var msgtxt = gk_text.replace(/\[/g, '<');
msgtxt = msgtxt.replace(/\]/g, '>');
Upvotes: 6