Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find the exact money in NodeJs from Statement

Here I have my money statements as MUR 30,000 or MUR 30000 or 30000 MUR or 30,000 MUR.

I have got already a regex which works for MUR 30000 and MUR 30,000.

/MUR \d+(,\d+)?/g

But now I need a regex that works for all four variants.

like image 602
be MrZulf Avatar asked Feb 03 '26 15:02

be MrZulf


2 Answers

You can use the following regex pattern:

/(?:MUR\s)?\d{1,3}(?:,\d{3})*(?:\sMUR)?/g

(?:MUR\s)?: Match

  • es "MUR " if it appears at the beginning. The (?: ... ) is a non-capturing group, and the ? makes it optional
  • \d{1,3}: Matches 1 to 3 digits (?:,\d{3})*: Matches groups of three digits preceded by a comma, zero or more times
  • (?:\sMUR)?: Matches " MUR" if it appears at the end
like image 193
m.piras Avatar answered Feb 06 '26 03:02

m.piras


You could match the digits part once, and then check if there is either MUR before or after it using lookaround assertions.

Note that you can surround the pattern with word boundaries \b to prevent partial word matches.

\d+(?:,\d+)?(?:(?<=MUR [\d,]+)|(?= MUR))

Regex demo

const regex = /\d+(?:,\d+)?(?:(?<=MUR [\d,]+)|(?= MUR))/g;
const s = `Here I have statements here my money in statement as MUR 30,000 or MUR 30000 or 30000 MUR or 30,000 MUR 3,0`;
console.log(s.match(regex));

Or use 2 alternations

 (?<=MUR )\d+(?:,\d+)?|\d+(?:,\d+)?(?= MUR)

Regex demo

const regex = /(?<=MUR )\d+(?:,\d+)?|\d+(?:,\d+)?(?= MUR)/g;
const s = `Here I have statements here my money in statement as MUR 30,000 or MUR 30000 or 30000 MUR or 30,000 MUR 3,0`;
console.log(s.match(regex));
like image 34
The fourth bird Avatar answered Feb 06 '26 03:02

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!