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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:42:52+00:00 2026-06-04T20:42:52+00:00

note: I’m -not- trying to parse HTML with regex I’m trying to replace any

  • 0

note: I’m -not- trying to parse HTML with regex

I’m trying to replace any content wrapped in $ signs ($for example$) in a string. I’ve managed to come up with str.replace(/\$([^\$]*)\$/g), "hello $1!"), but I’m having issues with making sure I don’t replace such strings when they are wrapped in HTML tags.

Example string: $someone$, <a>$welcome$</a>, and $another$

Expression: /[^>]\$([^\$]*)\$[^<]/g

Expected output: hello someone!, <a>$welcome</a>, and hello another!

Actual output: $someonhello , !elcomhello , and !nother$

Test code: alert("$someone$, <a>$welcome$</a>, and $another$".replace(/[^>]\$([^\$]*)\$[^<]/g, "hello $1!"));

fiddle:
http://jsfiddle.net/WMWHZ/

Thanks!

  • 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-06-04T20:42:54+00:00Added an answer on June 4, 2026 at 8:42 pm

    Keep in mind that you have 6 ‘$’ in your test case. The problem here is that when you try to check if the previous character isn’t a ‘>’, the regexp moves forward and matches what’s between the 4th and the 5th dollar symbol, capturing "</a>, and " and making a mess.

    Try this one:

    $('div').text(test.replace(/(^|[^>])\$([^<][^\$]*)\$(?!<)/g, "$1hello $2!"))​
    

    Javascript doesn’t support lookbehinds in regular expressions, but it does support lookaheads (the (?!<) part). To emulate lookbehinds, you correctly tried to put [^>] before the dollar, but then the character is matched so you have to catch it and put it again in the string.

    You just have to refine it a little, because if the ‘$’ is at the beginning of the string, the group isn’t captured.

    Also, to avoid problems like the one above, you should check if there isn’t a ‘<‘ after the first dollar, so I put a [^<] at the beginning of the capturing group. This also mean that it won’t catch empty strings between dollar symbols (as in ‘$$’), they must contain at least one character.

    This way, you have the expected result.

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

Sidebar

Related Questions

NOTE: I am not a native English speaker. Please excuse any grammar mistakes. I
Note: A closure cannot call any user-defined function, because the function's context is not
Note: purely out of curiosity and not for any actual use case. I'm wondering
Note This is not a REBOL-specific question. You can answer it in any language.
Note: I do NOT want to use any framework. The goal is just to
NOTE: I'm ultra-newbie on algorithm analysis so don't take any of my affirmations as
Note: I've read this and its not quite what I'm looking for: I have
Note the tag: VBA , not VB6, not VB.NET. This is specific to VBA
Note my wordpress version is 3.3.1 some hooks and function is not the same
Note: I inherited this site, I did not build it. The home page is

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.