0

How can i keep looking the regex pattern after some string? For example... After the string "Tel.: ", find all numbers after it... Even if them are splited by another characters

"Tel: 324234 -- 2 123123 (22)" extract "324234212312322" (but using on regex expression, without another functions, or regexes)

I already accomplished to extract the first Numbers after "Tel.: "

(?<=Tel\.:\s)\d*

https://regexr.com/4sspo

As i said i would like to learn how to keep looking for the "\d" groups after the first group found

The fourth bird
  • 154,723
  • 16
  • 55
  • 70

3 Answers3

1

You can use the replace method and a regex with the g flag.

var str = "Tel: 324234 -- 2 123123 (22)";
var numString = str.replace(/\D/g, "");

numString will be "324234212312322". This can be a bit tricky for more complex patterns, but should still work if you're careful.

bbbbbb
  • 116
  • 6
0

Instead of using a lookbehind (which is not widely supported by all browers), you could match Tel with an optional dot, a colon and whitespace char \.?:\s and capture in a group \bTel\.?:\s(.+) what comes after it.

\bTel\.?:\s(.+)

Then for every first capture group, replace all non digits.

const regex = /\bTel\.?:\s(.+)/g;
const str = `Tel: 324234 -- 2 123123 (22)

11111111111111111111 Tel.: 145628 810 - 7469
`;
let m;

while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  console.log(m[1].replace(/\D+/g, ''));
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Capturing that with only regex is not possible, assuming you're saying capturing all numbers in an arbitrary format.

Since arbitrary format means you'll have an arbitrary number of splitters, means you need to match a group multiple times.

While in Javascript, you can only get the last group repeated in the result: https://stackoverflow.com/a/3537914/1772088

Hao-Cher Hong
  • 230
  • 2
  • 6