I'm getting the following String in Javascript:
Nome Sobrenome <[email protected]>
How do I get only the email that is within <>
??
I'm getting the following String in Javascript:
Nome Sobrenome <[email protected]>
How do I get only the email that is within <>
??
You can use a regex for this, or String.slice
.
It would look something like this:
var string = 'Nome Sobrenome <[email protected]>';
var varianteA = string.slice(
string.indexOf('<') + 1,
string.indexOf('>')
);
console.log(varianteA);
var varianteB = string.match(/<([^>]+)>/);
varianteB = varianteB ? varianteB[1] : '';
console.log(varianteB);
About regex:
<
at the beginning of the part to be found (
- all but [^>]+
1 or more times >
end of part to be found in string Then I used )
if there is no >
and avoid errors before trying to access varianteB = varianteB ? varianteB[1] : '';
if the match gives match
.
You can use regex
...
alert("<[email protected]>".match(/\<([^)]+)\>/)[1]);
I'll post it because it already started @Sergio is very fast