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

9

I have the following string:

var text = "Meu nome <[email protected]>";

I would like to only get [email protected] , using Regex.

    
asked by anonymous 30.01.2017 / 14:19

4 answers

4

Using the Javascript regex API:

var text = "Meu nome <[email protected]>";
var regex = /(<)(.*)(>)/;

console.log(regex.exec(text)[2]);

Explaining

  • // delimits the area to execute a regex
  • (<) looks for a string that begins with <
  • (.*) Any character within <
  • % with end of clipping ending with >

This regex will return a result of three groups, so the (>) , which will only get the email .

    
30.01.2017 / 14:44
6

You can do this:

var text = "Meu nome <[email protected]>";
var email = text.replace(/.*<(.*)>.*/, '$1');

console.log(email);

Note that $ 1 represents (.*) , between < and > , which in this case is email.

You can find more information about regex here .

    
30.01.2017 / 14:24
3

The ideal for these cases is to think as much of the information as possible that will generate a catch pattern.

Example

You said you want what's between < and >

  • Do you consider it an email? yea? then you could define that it should have a @ and which special characters will be valid, eg _-.
  • If it's not an email, do you think you can capture > itself? (his final character). e.g. <var1 > var2>

Resolution

As I do not know exactly what your catch is I'll consider the ones I quoted above.

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

All the results you want are in group 1.

Note

**: reminding you that you can add special characters to parts of [ ... ] . ex. [a-z_-.]

Addendum

If you want to capture several at a time, you just need to flag g at the end of the regex.

Use

var text ="Meu nome <[email protected]>";
var r = /<([^>]+)>/
console.log(r.exec(text)[1])
    
31.01.2017 / 11:51
1

I know the question is already answered, but what if I wanted to get more than one email? These regular expressions are only working for an email.

I have done the function below, which although it does not use regular expressions, has the functionality of locating and saving 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>
    
30.01.2017 / 17:45