admin管理员组文章数量:1187852
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.
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.
Share Improve this question asked Sep 18, 2014 at 10:57 Hassene BenammouHassene Benammou 3792 gold badges4 silver badges11 bronze badges 3 |4 Answers
Reset to default 21Short 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();
- https://github.com/mathiasbynens/regenerate
- https://github.com/mathiasbynens/unicode-7.0.0
Watch:
- https://www.youtube.com/watch?v=zi0w7J7MCrk
\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:
/[$£]/
You could use an addon like XregExp.
You can also use this /(kr|$|£|€)/
.
currencySymbol = format.match(/(kr|$|£|€)/);
本文标签: Javascript regex Currency symbol in a stringStack Overflow
版权声明:本文标题:Javascript regex Currency symbol in a string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738378053a2083756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
format.charAt(0)
? – Alex K. Commented Sep 18, 2014 at 10:59