user1799242
user1799242

Reputation: 525

Regular expression to match ALL currency symbols?

Is there a regular expresion that will match ALL currency symbols (not just $) as they appear in HTML? I am trying to extract all occurrences of money amounts from an html page. Thanks!

Upvotes: 18

Views: 16454

Answers (4)

Vishal Naikawadi
Vishal Naikawadi

Reputation: 493

While searching for the same solution I came across this question. So I came up with my own solution in JS. This might not be ideal for everyone as all I'm trying to do is ignore the digits, commas, and dots in the given string.

function extractCurrencySymbol (currencyString) {
            // Regular expression to match any currency symbol
            let currencyRegex = /[^\d.,\s]/;
            
            // Use match method to find the currency symbol in the string
            let match = currencyString.match(currencyRegex);
            
            // Check if a match is found
            if (match) {
                // Return the matched currency symbol
                return match[0];
            } else {
                // Return null if no currency symbol is found
                return null;
            }
        }

Upvotes: 0

Black
Black

Reputation: 10407

JavaScript doesn't support PCRE regex property \p{Sc}

You need to use equivalent

function getCurrencySymbol(price: string): string {
  const ScRe =
    /[\$\xA2-\xA5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20BD\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6]/;

  return price.match(ScRe)[0];
}


// returns "£"
getCurrencySymbol("£10");

Upvotes: 0

Arash Yazdani
Arash Yazdani

Reputation: 302

I converted all of currency symbols to Unicode and If you want to check it manually, I suggest you to use this code:

[\u00a4\u060b\u0e3f\u002f\u002e\u20bf\u20b5\u00a2\u20a1\u0024\u0434\u0435\u043d\u0438\u043d\u0024\u20ab\u20ac\u0192\u002c\u20b2\u20b4\u20ad\u010d\u043b\u0432\u20ba\u20a5\u20a6\u0024\u20b1\u00a3\u17db\u20bd\u20b9\u20a8\u20aa\u09f3\u20ae\u20a9\u00a5\u0142\u20b3\u20a2\u20b0\u20bb\u20af\u20a0\u20a4\u2133\u20a7\u211b\u20b7\u20b6\u09f2\u09f9\u09fb\u07fe\u07ff\u0bf9\u0af1\u0cb0\u0dbb\u0dd4\ua838\u1e2f\u0046\u0061-\u007a\u0041-\u005a\u0030-\u0039 \u2000-\u200f\u2028-\u202f\u0621-\u0628\u062a-\u063a\u0641-\u0642\u0644-\u0648\u064e-\u0651\u0655\u067e\u0686\u0698\u06a9\u06af\u06be\u06cc\u06f0-\u06f9\u0629\u0643\u0649-\u064b\u064d\u06d5\u0660-\u0669\u005c]{1,5}

Upvotes: 1

hd1
hd1

Reputation: 34677

\p{Sc} seems to be the magic incantation you are looking for.

Upvotes: 45

Related Questions