Home > Net >  regex to get multiple values in javascript
regex to get multiple values in javascript

Time:10-03

my string is

Cuenta de Ahorros | 1231231231 Bs 102,423.88 Cuenta de Ahorros | 1231233123 Usd 102,423.88 Cuenta de Ahorros | 87316633 Usd 10.233 Cuenta de Ahorros | 49189387113313 Usd 12.301

but using match() i can't capture all string values

const strings = [
  "Cuenta de Ahorros | 1231231231 Bs 102,423.88 Cuenta de Ahorros | 1231233123 Usd 102,423.88 Cuenta de Ahorros | 87316633 Usd 10.233 Cuenta de Ahorros | 49189387113313 Usd 12.301"
]; 

let pattern = /(?<=Bs).*?(?=Cuenta)/;
strings.forEach((s) => {
  console.log(s.match(pattern));
});

returns only

[" 102,423.88 ", 40, "Cuenta de Ahor...] 0 : " 102,423.88 "

i need result like

Bs 102,423.88

Usd 102,423.88

Usd 10.233

Usd 12.301

CodePudding user response:

You can use

const strings = [
  "Cuenta de Ahorros | 1231231231 Bs 102,423.88 Cuenta de Ahorros | 1231233123 Usd 102,423.88 Cuenta de Ahorros | 87316633 Usd 10.233 Cuenta de Ahorros | 49189387113313 Usd 12.301"
]; 

let pattern = /\b(?:Bs|Usd)\b.*?(?=\s*Cuenta\b|$)/gi;
strings.forEach((s) => {
  console.log(s.match(pattern));
});

See the regex demo.

Pattern details:

  • \b(?:Bs|Usd)\b - a whole word Bs or Usd
  • .*? - any zero or more chars other than line break chars, as few as possible
  • (?=\s*Cuenta\b|$) - a positive lookahead that requires (immediately to the right of the current location) either zero or more whitespaces and then a whole word Cuenta (\s*Cuenta\b|$), or end of string ($).

I added g flag and used String#match to extract all matches from a string, and i flag makes the search case-insensitive.

CodePudding user response:

As you also want to match the dollar variant, you could write the pattern including matching Bs and Usd as:

\b(?:Bs|Usd)\s (?:\d{1,3}(?:,\d{3})*(?:\.\d )?|\d{4,})(?=\s Cuenta\b|$)

Explanation

  • \b A word boundary to prevent a partial word match
  • (?:Bs|Usd)
  • \s Match 1= whitespace chars
  • (?: Non capture
    • \d{1,3} Match 1-3 digits
    • (?:,\d{3})* Optionally repeat , and 3 digits
    • (?:\.\d )? Optionally match . and 1 digits
    • | Or
    • \d{4,} Match 4 or more digits
  • ) Close non capture group
  • (?=\s Cuenta\b|$) Assert either 1 whitespace chars and Cuenta to the right or assert the end of the string

Regex demo

const pattern = /\b(?:Bs|Usd)\s (?:\d (?:,\d{3})*(?:\.\d )?|\d{4,})(?=\s Cuenta\b|$)/g;
const strings = [
  "Cuenta de Ahorros | 1231231231 Bs 102,423.88 Cuenta de Ahorros | 1231233123 Usd 102,423.88 Cuenta de Ahorros | 87316633 Usd 10.233 Cuenta de Ahorros | 49189387113313 Usd 12.301"
];

strings.forEach((s) => {
  console.log(s.match(pattern));
});

Or using matchAll with a capture group:

const pattern = /(\b(?:Bs|Usd)\s (?:\d{1,3}(?:,\d{3})*(?:\.\d )?|\d{4,}))(?:\s Cuenta\b|$)/g;
const strings = [
  "Cuenta de Ahorros | 1231231231 Bs 102,423.88 Cuenta de Ahorros | 1231233123 Usd 102,423.88 Cuenta de Ahorros | 87316633 Usd 10.233 Cuenta de Ahorros | 49189387113313 Usd 12.301"
];

strings.forEach((s) => {
  console.log(Array.from(s.matchAll(pattern), m => m[1]));
});

  • Related