regex only looks for patterns in a text, but the expression itself does not substitute. If you want to manipulate the found result, an alternative would be to first fetch the numbers you want (with the other characters - dash, spaces, etc.) and then delete the characters that are not digits:
let texto = `Casa - CEP 0334211 - Jardim Belém, Suzano, São Paulo
Quem recebe: Julio Oliveira - Tel.: 145628 810 - 7469
Eu queria retirar 145628 + 810 + 7469
Ou seja: "1456288107469"
Só em 1 regex
Eu consigo fazer isso com outros passos mas gostaria de aprender a usar só 1 regex`;
//buscar os números depois de "Tel"
let match = texto.match(/Tel\.:((?:[^\d\n\r]*\d+)+)/);
if (match) {
// eliminar os caracteres que não são dígitos
console.log(match[1].replace(/\D+/g, '')); // 1456288107469
}
Note that I don’t need to put "Tel.:" inside a lookbehind, I find it unnecessary. Instead, I put the section I want to capture in parentheses, because it forms a catch group. And since it is the first pair of parentheses, then it will be group 1, which I can capture later in the first position of the array match
(within the if
, when I do match[1]
).
Then I use a character class denied: [^\d\n\r]
. This excerpt takes any character that nay be a digit (\d
) or a line break (\n
and \r
). I did this because I understood that after "Tel.:", all the numbers are on the same line, and you want to catch them all until the end of this line. I also put the quantifier *
(zero or more occurrences), indicating that they may or may not have several of these characters before a number.
Then we have \d+
(one or more digits). You had used \d*
, but this expression means "zero or more digits" (that is, if it has no digit, it also serves). Using +
, you ensure that you must have at least one digit.
This whole sequence (non-digits followed by digits) also has a +
soon after, indicating that it can repeat itself several times (and the use of +
ensures that it must occur at least once).
That is, everything after "Tel.:" (provided it is non-digits followed by digits, repeated several times, until the end of the line) will be in group 1. Then just take the value of group 1 (match[1]
) and delete anything that is not digit (the \D+
within the replace
- and note the use of flag g
, for all occurrences to be replaced). What is left are only the numbers.