JavaScript Regex exact match on number, not digit regex exact match python,regex exact match string,regex exact

I have a scenario where I need to find and replace a number in a large string using javascript. Let's say I have the number 2 and I want to replace it with 3 - it sounds pretty straight forward until I get occurrences like 22, 32, etc.

The string may look like this:
"note[2] 2 2_ someothertext_2 note[32] 2finally_2222 but how about mymomsays2."

I want turn turn it into this:
"note[3] 3 3_ someothertext_3 note[32] 3finally_2222 but how about mymomsays3."

Obviously this means .replace('2','3') is out of the picture so I went to regex. I find it easy to get an exact match when I am dealing with string start to end ie: /^2$/g. But that is not what I have. I tried grouping, digit only, wildcards, etc and I can't get this to match correctly.

Any help on how to exactly match a number (where 0 <= number <= 500 is possible, but no constraints needed in regex for range) would be greatly appreciated.

Answer:1

The task is to find (and replace) "single" digit 2, not embedded in a number composed of multiple digits.

In regex terms, this can be expressed as:

  • Match digit 2.
  • Previous char (if any) can not be a digit.
  • Next char (if any) can not be a digit.

The regex for the first condition is straightforward - just 2. In other flavours of regex, e.g. PCRE, to forbid the previous char you could use negative lookbehind, but unfortunately Javascript regex does not support it.

So, to circumvent this, we must:

  • Put a capturing group matching either start of text or something other than a digit: (^|\D).
  • Then put regex matching just 2: 2.

The last condition, fortunately, can be expressed as negative lookahead, because even Javascript regex support it: (?!\d).

So the whole regex is:

(^|\D)2(?!\d)

Having found such a match, you have to replace it with the content of the first capturing group and 3 (the replacement digit).

Answer:2

You can use negative look-ahead:

(\D|^)2(?!\d)

Replace with: ${1}3

If look behind is supported:

(?<!\d)2(?!\d)

Replace with: 3

Answer:3

I am attempting to set up two analytics trackers in my Angular App. One of my requirements is to set up a tracker for every user that logs in to the app that forwards page tracking and event data to a ...

I am attempting to set up two analytics trackers in my Angular App. One of my requirements is to set up a tracker for every user that logs in to the app that forwards page tracking and event data to a ...

  1. angularjs google analytics event tracking
  2. angularjs google analytics
  3. angularjs google analytics example
  4. angularjs google analytics service
  5. angularjs single page google analytics
  6. angularjs directive for google analytics
  7. google analytics angularjs ui-router
  8. google analytics angularjs typescript
  9. google analytics angularjs controller

Following is my pseudo code: function main() { return one() || two(); } function one() { console.log("one"); } function two() { console.log("two"); } main(); I recently studied that in ...

Following is my pseudo code: function main() { return one() || two(); } function one() { console.log("one"); } function two() { console.log("two"); } main(); I recently studied that in ...

I have tried different CSS techniques to change the font size of text in C3 charts. But when I use CSS, the text gets cut off beyond certain font-size (ex. 14px). I am using horizontal bar-chart with ...

I have tried different CSS techniques to change the font size of text in C3 charts. But when I use CSS, the text gets cut off beyond certain font-size (ex. 14px). I am using horizontal bar-chart with ...

  1. increasing font size in outlook
  2. increasing font size on mac
  3. increasing font size in windows 10
  4. increasing font size on iphone
  5. increasing font size latex
  6. increasing font size in gmail
  7. increasing font size html
  8. increasing font size on macbook pro
  9. increasing font size on ipad
  10. increasing font size css
  11. increasing font size on computer
  12. increasing font size in safari
  13. increasing font size of notes in powerpoint
  14. increasing font size in outlook 365
  15. increasing font size in ggplot
  16. increasing font size in outlook 2016
  17. increasing font size in pdf
  18. increasing font size in outlook reading pane
  19. increasing font size in quickbooks
  20. increasing font size in overleaf

Does it matter when we use the preventDefault method? (right in the beginning of a function vs at the end). All the tutorials that I have watched place preventDefault at the end of the function, but I ...

Does it matter when we use the preventDefault method? (right in the beginning of a function vs at the end). All the tutorials that I have watched place preventDefault at the end of the function, but I ...

  1. does it matter when you take a pregnancy test
  2. does it matter when you eat
  3. does it matter when you take creatine
  4. does it matter when you sleep
  5. does size matter when getting pregnant
  6. does position matter when trying to conceive
  7. does it matter when you eat protein
  8. does kilometers matter when buying a car
  9. does it matter when you take protein
  10. does it matter when you work out
  11. does it matter when you post on instagram
  12. does it matter when you take vitamins
  13. does it matter when you eat your calories
  14. does it matter when i take a pregnancy test
  15. does age matter when dating
  16. does it matter when you drink protein
  17. does it matter when i take creatine
  18. does it matter when you start birth control
  19. does cp matter when evolving
  20. does it matter when you eat carbs