A way to go is to match the parts around the comma and then remove it:

var input = '124,000 km';
input.replace(/(\d+),(\d+\s?km)/g, '2');
Answer from gion_13 on Stack Overflow
🌐
DSPACE
darrengoossens.wordpress.com › 2017 › 11 › 10 › simple-use-of-word-regular-expressions-remove-commas-from-numbers
Simple use of Word regular expressions — remove commas from numbers
November 10, 2017 - Note to self: Our house style says we should us nonbreaking spaces in numbers of five or more digits, not commas. That is, a number like 13,456 should be 13 456. If I get a document with lots of commas in, this is what I do in Word: Wildcard replace (1) Turn off track changes (sad…
Discussions

regex - remove comma from a digits portion string - Stack Overflow
How can I (fastest preferable) remove commas from a digit part of a string without affecting the rest of the commas in the string. So in the example below I want to remove the comas from the number More on stackoverflow.com
🌐 stackoverflow.com
August 26, 2012
c# - Removing commas from numbers with .NET regex - Stack Overflow
So I'm processing a report that (brilliantly, really) spits out number values with commas in them, in a .csv output. Super useful. So, I'm using (C#)regex lookahead positive and lookbehind positive More on stackoverflow.com
🌐 stackoverflow.com
July 7, 2018
Replacing space and comma from a string using regex
I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
0
October 26, 2020
remove comma separation from the specific number from JS
I want to remove comma separation from the specific number but i want decimal places and i wrote a regular expression for that, when i pass a number with decimal places it is ok. var num = '12,312... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › How-to-remove-comma-between-the-numbers › td-p › 752561
Solved: How to remove comma between the numbers - Alteryx Community
September 29, 2023 - A comma between numbers doesn't exist in reality. It is only showing to make it easier for you to read the numbers. ... Okay. Thanks lol ... You can use Data cleansing tool to remove character like comma or any other punctuation. Comma can also be removed using regex tool by creating regular ...
Top answer
1 of 2
3

You may use any of the solutions below:

var s = "abc,def,2,100,xyz!,:))))";
Console.WriteLine(Regex.Replace(s, @"(\d),(\d)", "2"));   // Does not handle 1,2,3,4 cases
Console.WriteLine(Regex.Replace(s, @"(\d),(?=\d)", "$1"));   // Handles consecutive matches with capturing group+backreference/lookahead
Console.WriteLine(Regex.Replace(s, @"(?<=\d),(?=\d)", ""));  // Handles consecutive matches with lookbehind/lookahead, the most efficient way
Console.WriteLine(Regex.Replace(s, @",(?<=\d,)(?=\d)", "")); // Also handles all cases

See the C# demo.

Explanations:

  • (\d),(\d) - matches and captures single digits on both sides of , and 2 are replacement backreferences that insert captured texts back into the result
  • (\d),(?=\d) - matches and captures a digit before ,, then a comma is matched and then a positive lookahead (?=\d) requires a digit after ,, but since it is not consumed, onyl $1 is required in the replacement pattern
  • (?<=\d),(?=\d) - only such a comma is matched that is enclosed with digits without consuming the digits ((?<=\d) is a positive lookbehind that requires its pattern match immediately to the left of the current location)
  • ,(?<=\d,)(?=\d) - matches a comma and only after matching it, the regex engine checks if there is a digit and a comma immediately before the location (that is after the comma), and if the check if true, the next char is checked for a digit. If it is a digit, a match is returned.

RegexHero.net test:

Bonus:

You may just match a pattern like yours with \d,\d and pass the match to the MatchEvaluator method where you may manipulate the match further:

Console.WriteLine(Regex.Replace(s, @"\d,\d", m => m.Value.Replace(",",string.Empty))); // Callback method

Here, m is the match object and m.Value holds the whole match value. With .Replace(",",string.Empty), you remove all commas from the match value.

2 of 2
0

You can always check a website that evaluates regex expressions. I think this code might be able to help you:

str = Regex.Replace(str, @",)(?<=(\d))", "");
🌐
PyTutorial
pytutorial.com › remove-comma-in-number-python
PyTutorial | How to Remove Comma in Number Python
February 15, 2023 - Now let's see how to use replace() to remove commas from a number.
🌐
freeCodeCamp
forum.freecodecamp.org › programming › javascript
Replacing space and comma from a string using regex
October 26, 2020 - I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ...
Find elsewhere
🌐
Reddit
reddit.com › r/powershell › regex remove the second comma in a string
r/PowerShell on Reddit: Regex Remove the Second Comma in a string
July 27, 2021 -

If I have the following code:

$Description = 'Value1, 20212707.1, Testing a description, today!'
$Description = $Description -creplace '[,]*,', '' 

Write-Host $Description

Line two uses Regex to find the first comma in the $Description variable. As I am not familiar with all regex, is there a function that could find the second comma and then replace it with my empty string? It would be nice to know if there is a way to actually find the second command and the space thereafter.

Curious if this is possible and for help!

🌐
Stack Overflow
stackoverflow.com › q › 12126597
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers
🌐
Medium
frontendinterviewquestions.medium.com › how-to-remove-comma-from-string-in-javascript-21c88f166c28
How to remove comma from string in JavaScript | by Pravin M | Medium
March 13, 2024 - ->The first argument is a regular expression (/,/g). This expression matches any comma character (",") and the g flag ensures all occurrences are replaced.