0

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

1
  • There seems to be many things going on here: If you need Bs in the output, and Usd, too, you need a group: (Bs|Usd). If you need it in the match, why use lookbehind? Use a consuming pattern. To extract multiple matches, use g flag in the regex with String#match Commented Oct 2, 2022 at 19:32

2 Answers 2

1

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]));
});

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.