简体   繁体   中英

Javascript regex for currency format

Suppose I have an unknown price format, that can be

12345.00 USD

123.00 XXX

anyOtherString

..and I need to have

12345.00 USD ==> $12345

12345 00 USD ==> $123

123.00 XXX ==> 123.00 XXX

anyOtherString ==> anyOtherString

How would you approach this?

EDIT

Testing with the following:

 var str1 = "122.00 USD"; console.log("str1: "+str1.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2')); // ==> $122 var str2 = "122 00 USD"; console.log("str2: "+str2.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2')); // ==> $122 var str3 = "122 USD"; console.log("str3: "+str3.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2')); // ==> $122 var str4 = "122"; console.log("str4: "+str4.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2')); // ==> 122 var str5 = "122.00"; console.log("str5: "+str5.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2')); // ==> 122.00 var str6 = "$122"; console.log("str6: "+str6.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2')); // ==> $122 

..and it's almost OK, except for str4 and str5 that gave a wrong result

Edit : The correct answer is in the edit part, below

Here is something that works for every case I could think about :

Replace :
(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD
by
$\\1\\2

However, if there is a missing point between in a non integer number, it doesn't work. For instance, 123 456 USD will be replaced by 123 $456
Also, I couldn't find a way to let numbers like "123.456 USD" untouched (see comments below)

Demo here

I can provide more details if you need any explanation.
If you see some cases I didn't handle, just say it !

Code for Javascript :

 var str = '123.00 USD 1234 USD 357.0 USD 456 00 USD 651USD 753.684 USD 123 456 USD -123 USD 789.00 XXX 123.00 wrongcurrency test string $159'; str = str.replace(/(\\d+)(?:(?:[\\.\\s]0+|(\\.\\d+)))?\\s?USD/g, '$$$1$2'); console.log(str); 


EDIT :

Ok I think I got it. Correct me if I am wrong :
Your string is composed by a single number, possibly with a fractional part separated by either a dot or a space. The number may be followed by an other string.

If the fractional part of this number doesn't exist, or equals 0, and this number is followed by USD or nothing, you want to replace it by the same value with a $ at the beginning. Otherwise, you do nothing.

If this is what you're looking for, I can give you exactly what you want (it is far more simple than multiple numbers in the same string ^^).
123 , 123.00 , 123 USD , 123 00 USD and -123 00 have to be matched and replaced for example

You just have to replace :
^(-)?(\\d+)(?:[\\s\\.]0+)?\\s*(?:USD)?$
by
\\1$\\2

Demo here

Code for Javascript :

 var strArray = ['123.00 USD', '1234 USD', '357.0 USD', '456 00 USD', '651USD', '753.684 USD', '123 456 USD', '-123 USD', '789.00 XXX', '123.00 wrongcurrency', 'test string', '$159', '123']; var strReplaced = ""; for (var i = 0; i < strArray.length; ++i) { strReplaced = strArray[i].replace(/^(-)?(\\d+)(?:[\\s\\.]0+)?\\s*(?:USD)?$/g, '$1$$$2'); console.log(strArray[i]+" => "+strReplaced); } 

Hi you could try the following

([$ \\s] (?:\\d{1,3}(?:[\\s,]\\d{3})+|\\d+)(?:.\\d{2})?([\\s]*[azAZ]{0,3}))

example : https://regex101.com/r/ngWwDE/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM