Regular expression for separating thousands separator from a numeric string?

I have strings that contain thousands of separators, however the string-to-number function does not want to use it correctly (using JavaScript). I am thinking about β€œpreparing” a line by dividing all the thousands of separators, leaving everything else open and allowing the functions Number/ parseInt/ parseFloat(I am satisfied with their behavior differently) to solve everything else. But it seems to me that I have no idea that RegExp can do this!

Better ideas are welcome too!


UPDATE:

Sorry, the answers enlightened me as a poorly worded question. What I get is: 1) to cut thousands of delimiters only if they are, but 2) not to disturb the original string, so I get NaNs in case of invalid digits.

MORE UPDATES:

JavaScript is limited to English for parsing, so it can be assumed that the thousands separator is used for the separator. ',' for simplicity (naturally, it never matches the decimal separator in any locale, so switching to any other language should not be a problem)

Now, when analyzing the functions:

parseFloat('1023.95BARGAIN BYTES!')  // parseXXX functions just "gives up" on invalid chars and returns 1023.95
Number('1023.95BARGAIN BYTES!')      // while Number constructor behaves "strictly" and will return NaN

Sometimes I use rhw, free, sometimes strict. I want to find the best approach to prepare the string for both functions.

About the reality of the numbers:

'1,023.99' - , . '1,0,2,3.99' , '1023.99', .

+5
7

welp, :

:

stringWithNumbers = stringwithNumbers.replace(/(\d+),(?=\d{3}(\D|$))/g, "$1");

1,234,567.12
1,023.99
1,0,2,3.99
the dang thing costs $1,205!!
95,5,0,432
12345,0000
1,2345

1234567.12
1023.99
1,0,2,3.99
the dang thing costs $1205!!
95,5,0432
12345,0000
1,2345

, !

EDIT:

, , :

(\b\d{1,3}),(?=\d{3}(\D|$))

" " (+) "-" ({1,3}) "-". , 1234,123 == > 1234123. , , ( ), A123,789 _1,555 ( ).

+7

num.replace(/,/g, '') , .

+4

,

myString = myString.replace(/[ ,]/g, "");

.

+2

var decimalCharacter = ".",
    regex = new RegExp("[\\d" + decimalCharacter + "]+", "g"),
    num = "10,0000,000,000.999";
+num.match(regex).join("");
+1

, :

/^(\d*|\d{1,3}(,\d{3})+)($|[^\d])/.test(numeral_string)

true, - (1) , (2) (3) , , , . ( β„–3 , "BARGAIN BYTES!".)

, :

numeral_string.replace(/,/g, '')

.

+1

, :

numberStr = numberStr.replace(/[. ,](\d\d\d\D|\d\d\d$)/g,'$1');

, .

In some cases (for example, a price of 1000.5 euros) the above does not work. If you need something more reliable, this should work 100% of the time:

//convert a comma or space used as the cent placeholder to a decimal
$priceStr = $priceStr.replace(/[, ](\d\d$)/,'.$1');
$priceStr = $priceStr.replace(/[, ](\d$)/,'.$1');
//capture cents
var $hasCentsRegex = /[.]\d\d?$/;
if($hasCentsRegex.test($priceStr)) {
    var $matchArray = $priceStr.match(/(.*)([.]\d\d?$)/);
    var $priceBeforeCents = $matchArray[1];
    var $cents = $matchArray[2];    
} else{
    var $priceBeforeCents = $priceStr;
    var $cents = "";
}
//remove decimals, commas and whitespace from the pre-cent portion
$priceBeforeCents = $priceBeforeCents.replace(/[.\s,]/g,'');
//re-create the price by adding back the cents
$priceStr = $priceBeforeCents + $cents;
+1
source

you can use s.replaceAll("(\\W)(?=\\d{3})","");

This regular expression receives all alphanumeric characters with three characters after it.

Type strings 4.444.444.444,00 β‚¬will be4444444444,00 β‚¬

+1
source

All Articles