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

  • Home
  • SEARCH
  • 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 6819791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:20:54+00:00 2026-05-26T21:20:54+00:00

What am I doing wrong here? I’m trying to replace a number in a

  • 0

What am I doing wrong here?

I’m trying to replace a number in a string with another number using javascript. I have a long string that has the number 1 in it several times. I need to replace the number 1 with 2 in every case except where 1 has another number on either side. I did a bunch of google searches for how to use regex (I’m totally new to regex) and I came up with this.

string.replace(/(?<!\d)1(?!\d)/,2);

Basically, I want the regex to match (and thus replace) every occurrence of the number 1 where it is surrounded by anything except another number. I don’t want the match to include the surrounding characters–only the number 1.

I keep getting the invalid quantifier error in my firebug console. What am I doing wrong?

  • 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-26T21:20:54+00:00Added an answer on May 26, 2026 at 9:20 pm

    It’s this bit: (?<!\d). There’s no (?<, only (?:, (?=, and (?!.

    JavaScript doesn’t have look-behind, but I think you can work around it in this case, like this:

    str = str.replace(/(^|\D)1(?!\d)/g, "$12")
    

    That captures the character immediately prior to the digit, then echoes it back ($1 in the replacement string) followed by the new content (2). The ^ near the beginning allows for the digit being the first character in the string.

    Live example

    Breaking it down:

    (^|\D)   Match either start-of-string, or a non-digit, and capture the result
    1        Match the digit 1...
    (?!\d)   ...but only if it isn't followed by a digit

    And in the replacement, $12 is not “replace with capture group 12” (which is what it looks like to me), but “replace with capture group 1 followed by the digit 2.”

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

Sidebar

Related Questions

Someone has to be able to explain what I'm doing wrong here! I'm trying
what am I doing wrong here guys? $string = string How Long is a
what am I doing wrong here? I'm trying to create a DataTemplate using a
What am I doing wrong here? I'm trying to get started with jQuery UI
What am I doing wrong here? private void SendMail(string from, string body) { string
What am I doing wrong here? I am trying to limit my xpath search
Wondering what I am doing wrong here. I am trying to pass an integer
I have a feeling I'm doing something wrong here, but I'm not quite sure
What am I doing wrong here? I am trying to get the background color
What am I doing wrong here? public class SimpleSearchTest { public static void main(String[]

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.