How to pick a String that is between Javascript tags using Regex

Asked

Viewed 3,526 times

10

4 answers

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]);

Explaining

  • // 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.

  • 1

    Cool guy, this way is more performative, I will use it now, follow the test: https://jsperf.com/regex-test2017

  • 1

    Yes @Juniornunes, and it gets better as the amount of conditions increase.

  • @Lucascosta which are the 3 groups? I have this curiosity.

  • The return will be an array of 3, with <, e-mail and >. If you give a console.log(regex.exec(text)) it is possible to view @Douglas

  • 1

    Thank you @Lucascosta

  • 1

    I liked the solution and also the explanation =)

Show 1 more comment

4

Ideal for these cases is to think of as much information as possible that generates a capture pattern.

Example

You said you want what comes between < and >

  • Do you consider it to be an email? Yes? then you could define that you should have one @ and which special characters will be valid, ex.: _-.
  • If it’s not an email, you consider that you can capture yourself >? (its final character). ex.: <var1 > var2>

Resolution

As I don’t know exactly what your catch is I will consider the ones I quoted above.

  • Email : /<([a-z]+@[a-z]+(\.[a-z]{2,3}){1,2})>/i. REGEX101 **
  • Delimited there > (char final) : /<([^>]+)>/. REGEX101
  • May have > in the middle : /<(.*)>/. REGEX101

All the results you want are in group 1.

Note

** : remembering that you can add special characters in the parts of [...]. ex.: [a-z_-.]

Addendum

If you want to capture several at once, just put the flag g at the end of the regex.

Utilizing

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

You are not signed in. Login or sign up in order to post.