I have a variable that I'm using to build a JavaScript function call, and JavaScript's .replace()
to surround the line of text with a span and onclick event. The .replace()
portion looks like this:
code.replace(/(\d{4}\s+)?(LOCAL|PARAMETER|GLOBAL)\s+USING\s+([\S]+)/g,
"<span class=\"natprint_popup\" onclick=\"getNaturalCode('"
+ lib
+ "','$3','@#test_prod_qual|',0,'Y'); return false;\">$&</span>");
The only problem is that the variable lib
contains a $
at the end some of the time; for example, lib == DPDRI$
. This causes the JavaScript on my page to break and I get output that breaks at the end of lib
and displays the rest of the Javascript function parameters as plain text:
,'DPDPDRNO','TEST',0,'Y'); return false;">
I've been looking fruitlessly for answers for a few days now. I've tried doing lib.replace(/\$/g, "\\$");
and the \$
is successfully making its way into the variable but it still breaks my code. It seems like the JavaScript engine is trying to interpret the $
at the end of lib
as a captured match and it's making it blow up. Anyone have any ideas how to make this work?