Home > Article > Web Front-end > js/ts - regular expression
Of course! Here is an example of how to use regular expressions (regex) in TypeScript:
// Função para validar e-mail usando regex function validarEmail(email: string): boolean { const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; return regex.test(email); } // Testando a função const email1 = "teste@example.com"; const email2 = "invalid-email@.com"; console.log(`${email1} é válido? ${validarEmail(email1)}`); // Saída: teste@example.com é válido? true console.log(`${email2} é válido? ${validarEmail(email2)}`); // Saída: invalid-email@.com é válido? false
This example shows how you can use regex to validate an email format in TypeScript. If you need more examples or explanations, feel free to ask!
by ChatGPT
The above is the detailed content of js/ts - regular expression. For more information, please follow other related articles on the PHP Chinese website!