How to Use Regular Expressions
I need to identify a pattern in regex and save it in a variable, how to
![No description](https://cdn.answeroverflow.com/1195201682120900658/image.png)
3 Replies
Basic I need this, how to salve the output in a variable ?
![No description](https://cdn.answeroverflow.com/1195207932531454003/image.png)
Solved with this code:
function extrairCPFDaString(texto) {
const regexCpf = /\b\d{11}\b/;
const resultado = texto.match(regexCpf);
if (resultado) {
const cpfEncontrado = resultado[0];
return cpfEncontrado;
} else {
return "CPF não encontrado no texto.";
}
}
const texto = "CPF: 12234455566\nWhatsApp: +556791440690";
const resultadoExtracao = extrairCPFDaString(texto);
return (resultadoExtracao);
![No description](https://cdn.answeroverflow.com/1195213849670996028/image.png)
Yep, good job!