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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:03:02+00:00 2026-06-15T10:03:02+00:00

Before I start, I know there are better ways than regex doing this (like

  • 0

Before I start, I know there are better ways than regex doing this (like tokenizers), that’s not what the question is about. I’m already stuck using regex, and it already works as I need to, except one special case, which is what I need advice on.

I need to scan through some JavaScript-like code and insert the new keyword in front of every object declaration. I already know the names of all objects that will need this keyword, and I know that none of them will have that keyword in the code before I start (so I don’t need to deal with repeated new words or guessing whether something is an object or not. For example, a typical line could look like this:

foo = Bar()

Where I would already know that Bar is a ‘class’ and would need ‘new’ for object declaration. The following regex does the trick:

for classname in allowed_classes:
    line = re.sub(r'^([^\'"]*(?:([\'"])[^\'"]*\2)*[^\'"]*)\b(%s\s*\()' % classname, r'\1new \3', line)

It works like a charm, even making sure not to touch classname when it’s inside a string (The first portion of the regex tells it to make sure there are even number of quotes before-hand – it’s a bit naive in that it will break with nested quotes, but I don’t need to handle that case). Problem is, class names could also have $ in them. So the following line is allowed as well if $Bar exists in allowed_classes:

foo = $Bar()

The above regex will ignore it, due to the dollar sign. I figured escaping it would do the trick, but this logic seems to have no effect on the above line even if $Bar is one of the classes:

for classname in allowed_classes:
    line = re.sub(r'^([^\'"]*(?:([\'"])[^\'"]*\2)*[^\'"]*)\b(%s\s*\()' % re.escape(classname), r'\1new \3', line)

I also tried escaping it by hand using \ but it has no effect either. Can someone explain why converting $ to \$ isn’t working and what could fix it?

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-15T10:03:03+00:00Added an answer on June 15, 2026 at 10:03 am

    The reason your current regex isn’t working is that you have a \b just before your class name. \b will match word boundaries, so only between word characters and non-word characters. For the string foo = Bar(), the \b will match between the space and the B, but for foo = $Bar(), the \b cannot match between the space and the $ because they are both non-word characters.

    To fix this, change \b to (?=\b|\B\$), here is the resulting regex:

    for classname in allowed_classes:
        line = re.sub(r'^([^\'"]*(?:([\'"])[^\'"]*\2)*[^\'"]*)(?=\b|\B\$)(%s\s*\()' % classname, r'\1new \3', line)
    

    By using a lookahead, you can handle both of the following cases:

    • classname does not start with $, so we want a word boundary before trying to match classname, the \b inside of the lookahead handles this
    • classname does start with $, so if the next character is a $ we want to match. I used \B\$ so it will only match if the character before the $ is not a word character, but this is probably unnecessary since I can’t think of any valid JS code where that would be the case
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

before I start I want to point out that I tagged this question as
Before i start if someone know a better way to do this please Share
Before I start I would just like everyone know that I did indeed spend
Before I start this question, I understand that every aspect of it is wrong.
Before I start know this: I am extremely new to Java and programming. How
Before I start, I should let you know that I'm hardly experienced in PHP.
I feel like I've seen this question asked before, but neither the SO search
Before i start, i know the MD5 is compromised (collision attack and speed of
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
I usually try to do TDD with not much analysis (no diagrams) before start

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.