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 6996017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:06:12+00:00 2026-05-27T20:06:12+00:00

I need to replace the price in JavaScript string, but only if it is

  • 0

I need to replace the price in JavaScript string, but only if it is not a value of an attribute. The result should look like this:

'<span title="$30,000,000.50">$30,000,000.50</span>' // original
'<span title="$30,000,000.50">xxx</span>' // desired result

'bla bla bla $30,000,000.50 bla bla bla' // original
'bla bla bla xxx bla bla bla' // desired result

I define the price as a dollar sign, followed by a combination of digits, dots and commas:

/\$[\d,.]+/

That works fine. But when I try to exclude the occurences where this pattern is wrapped in quotes, it starts to behave funny:

'<span title="$30,000,000.50">$30,000,000.50</span>'.replace(/(?!=\")(\$[\d,.]+)(?!\")/gi, 'xxx');
// <span title="xxx0">xxx</span>

Notice that it replaced the value of title attribute, but it kept the last zero.

Can you please correct my code and tell me what I do wrong? Thank you.

  • 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-27T20:06:12+00:00Added an answer on May 27, 2026 at 8:06 pm

    You need a negative look ahead and a negative look behind. Unfortunately, JavaScript doesn’t support look behinds. However, using a technique from: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript you can mimic this behavior.

    str.replace(/(\")?(\$[\d,.]+)(?!\")/gi, function ($0, $1) {
        return $1 ? $0 : 'xxx';
    });
    

    http://jsfiddle.net/nachito/Wfdq6/1/

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

Sidebar

Related Questions

I need to round and print a price like below, but this does not
I need to replace special characters from a string, like this: this.value = this.value.replace(/\n/g,'');
I need to replace a string like 123456 to ,123456 . I tried this.
I need to replace some string in a PHP code to make it look
I have a string. In this string i need replace all special characters (0-31
I need replace all ; to \n , but :%s/;/\n/gc not works
I need to replace character (say) x with character (say) P in a string,
I need to replace our Ajax Modal Popup controls with a JavaScript equivalent. We
I need to replace all & in a String that isnt part of a
I want create Word document from another Word document, but I need replace some

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.