Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 678057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:06:26+00:00 2026-05-14T01:06:26+00:00

I have a variable that I’m using to build a JavaScript function call, and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:06:26+00:00Added an answer on May 14, 2026 at 1:06 am

    See the Specifying a string as a parameter section of the replace() documentation on MDC:

    The replacement string can include the following special replacement patterns:

    Pattern    Inserts
    $$    Inserts a "$".
    ...
    $'    Inserts the portion of the string that follows the matched substring.
    ...
    

    Note that since the contents of your variable is being inserted as a single-quote-wrapped parameter to the function you’re calling from onclick, it will always be followed by a ‘ – so when it ends with a $, you’ll have inadvertently created a replacement pattern:

     ... onclick=\"getNaturalCode('DPDRI$' ...

    Now, you could just change how you quote that particular parameter. But to be safe, you should really escape the $ symbol:

     + lib.replace(/\$/g, "$$$$")

    The above modification will convert “DPDRI$” into “DPDRI$$” prior to its insertion into the replacement string, allowing the final replacement to contain a literal $.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a variable that only one function can write (let's call
I'm studying javascript these days and I have question. I have variable that contain
I have a variable that im define with another variable and some text. $title
I have a variable that has this string: <DIV><SPAN style=FONT-FAMILY: Tahoma; FONT-SIZE: 10pt>[If the
I have a variable that is definited by a POST call from a form
I have a variable that contains some text, some html, basically can be a
How can I determine using PHP code that, for example, I have a variable
I have a variable that is... $whatever = 5865/100; This is a text variable.
I have a variable that I have successfully saved and restored using onSaveInstanceState @Override
I have a variable that contains a string of text and html tags, such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.