Javascript regex Currency symbol in a string

后端 未结 4 1570
南旧
南旧 2020-12-11 06:56

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

相关标签:
4条回答
  • 2020-12-11 07:13

    You could use an addon like XregExp.

    0 讨论(0)
  • 2020-12-11 07:14

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

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

    0 讨论(0)
  • 2020-12-11 07:34

    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();
    
    • https://github.com/mathiasbynens/regenerate
    • https://github.com/mathiasbynens/unicode-7.0.0

    Watch:

    • https://www.youtube.com/watch?v=zi0w7J7MCrk
    0 讨论(0)
  • 2020-12-11 07:34

    \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:

    /[$£]/
    
    0 讨论(0)
提交回复
热议问题