8

so I have a formatting string that can be $#,###.00 or "£#,###.00 and I would like to get the currency symbol form it here is the code that I'm using:

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

I would like currencySymbol to be equal to "$" or "£" but it's not working currencySymbol is null.

3
  • format.charAt(0) ? Commented Sep 18, 2014 at 10:59
  • not sure if it's always the first symbol for euros for example it's the last symbol Commented Sep 18, 2014 at 11:00
  • @HasseneBenammou does my answer below help you out? What exactly are you trying to accomplish? If you are interested in finding the various currency symbols, take a look at CLDR. For example, unicode.org/repos/cldr-aux/json/26/main/en/currencies.json . If you want to add markups on the currency symbols, I can also help you out. Commented Nov 28, 2014 at 18:41

4 Answers 4

21

Short answer:

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

Long answer:

A JavaScript regular expression equivalent to /\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

The above has been generated by:

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

Watch:

Sign up to request clarification or add additional context in comments.

Comments

4

\p{Sc} is PCRE regex property and Javascript doesn't support it.

In Javascript you need to use specific symbols in character class to match them like this:

/[$£]/

2 Comments

should I list all the currencies possible? because it can be anything.
Unfortunately yes, you will need to list all possible currencies in this character class in JS.
2

You could use an addon like XregExp.

Comments

0

You can also use this /(kr|$|£|€)/.

currencySymbol = format.match(/(kr|$|£|€)/);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.