Javascript regex Currency symbol in string

so I have a format string that can be $#,###.00

or "£#,###.00

, and I would like to get the form of a currency symbol, here it is the code I am using:

 currencySymbol =  format.match(/\p{Sc}/);

      

I would like the currencySymbol to be "$" or "£" but it doesn't work currencySymbol is null.

+3


source to share


3 answers


You can use an addon like XregExp .



+1


source


The short answer is :

/[\$\xA2-\xA5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20BD\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6]/

Long answer :

JavaScript regex equivalent /\p{Sc}/

is:

ScRe = /[\$\xA2-\xA5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20BD\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6]/

ScRe.test("$"); // true
ScRe.test("£"); // true
ScRe.test("€"); // true

      



Above was created:

$ npm install regenerate
$ npm install unicode-7.0.0
$ node 
> regenerate().add(require('unicode-7.0.0/categories/Sc/symbols')).toString();

      

Clock:

+6


source


\p{Sc}

is a PCRE regex property and Javascript doesn't support it.

In Javascript, you need to use specific characters in a character class to match like this:

/[$£]/

      

+1


source







All Articles