10
I have the following string:
var text = "Meu nome <[email protected]>";
I’d like to take just the [email protected]
, using Regex.
10
I have the following string:
var text = "Meu nome <[email protected]>";
I’d like to take just the [email protected]
, using Regex.
7
You can do it like this:
var text = "Meu nome <[email protected]>";
var email = text.replace(/.*<(.*)>.*/, '$1');
console.log(email);
Note that $1 represents the (.*)
, between the <
and the >
, which in this case is the email.
You can find more information about regex here.
5
Using the Javascript regex API:
var text = "Meu nome <[email protected]>";
var regex = /(<)(.*)(>)/;
console.log(regex.exec(text)[2]);
//
limits the area to run a regex(<)
searches for a string that starts with <(.*)
Any character within <(>)
the end of the cut-off ending with > This regex will return a result of three groups, so the regex.exec(text)[2]
, that will only take the e-mail.
4
Ideal for these cases is to think of as much information as possible that generates a capture pattern.
You said you want what comes between <
and >
@
and which special characters will be valid, ex.: _-.
>
? (its final character). ex.: <var1 > var2>
As I don’t know exactly what your catch is I will consider the ones I quoted above.
/<([a-z]+@[a-z]+(\.[a-z]{2,3}){1,2})>/i
. REGEX101 **>
(char final) : /<([^>]+)>/
. REGEX101>
in the middle : /<(.*)>/
. REGEX101All the results you want are in group 1.
** : remembering that you can add special characters in the parts of [
...]
. ex.: [a-z_-.]
If you want to capture several at once, just put the flag g
at the end of the regex.
var text ="Meu nome <[email protected]>";
var r = /<([^>]+)>/
console.log(r.exec(text)[1])
Very cool your solution!
2
I know the question is already answered, but what if I wanted to get more than one e-mail? These regular expressions are only working for one email.
I made the function below, which although not using regular expressions, has the functionality to find and save the content between <
and >
of one or more occurrences.
<script>
var texto = "Meus emails são <[email protected]>,<[email protected]> e <[email protected]>";
// Função que localiza conteúdo(emails) entre < e > e guarda em array
function localizar_tags(texto)
{
var emails = new Array();
i = 0;
while(texto.search("<") != -1)
{
pos_inicio = texto.search("<");
pos_fim = texto.search(">");
email = texto.substring(pos_inicio+1, pos_fim);
emails[i]=email;
texto = texto.substring(pos_fim+1,texto.length);
i++;
}
return emails;
}
// Para testar a função
window.onload=function()
{
emails = localizar_tags(texto);
for(i=0;i<emails.length;i++)
{
console.log(emails[i]);
}
}
</script>
Browser other questions tagged javascript regex
You are not signed in. Login or sign up in order to post.
Cool guy, this way is more performative, I will use it now, follow the test: https://jsperf.com/regex-test2017
– JuniorNunes
Yes @Juniornunes, and it gets better as the amount of conditions increase.
– BrTkCa
@Lucascosta which are the 3 groups? I have this curiosity.
– Roknauta
The return will be an array of 3, with
<
, e-mail and>
. If you give aconsole.log(regex.exec(text))
it is possible to view @Douglas– BrTkCa
Thank you @Lucascosta
– Roknauta
I liked the solution and also the explanation =)
– fernandoocf