-2

I have a string that will be formatted something like ___<test@email.com>____ where the underscores is irrelevant stuff I don't need but varys in length. I need to select and store what is between the brackets.

My problem is that all of the sub string solutions I have seen operate off of a hard integer location in the string. But the start and end of the substring I want to select (the brackets) will never be the same.

So I thought if I could use something to find the location of the brackets then feed that to a substring solution that would work. But all of the ways I have found of identifying special characters only reports if there are special characters, not where they are.

Thanks in advance!

David Richards
  • 885
  • 1
  • 8
  • 7

3 Answers3

2

Here's a regex that should set you on your way.

let string = "asdf asdf asdf as <thing@stuff.com> jl;kj;l kj ;lkj ;lk j;lk";
let myMatches = string.match(/<.*>/g);
let myMatch = myMatches[0].slice(1).slice(0,-1);

The .match function returns an array of matches, so you can find multiple <stuff> entries.

There's probably a way to do it without the slicing, but that's all I've got for now.

SethWhite
  • 1,891
  • 18
  • 24
2

based on this answer

var text = '___<test@email.com>____';

var values = text.split(/[<>]+/);

console.log(values); // your values should be at indexes 1, 3, 5, etc...
Community
  • 1
  • 1
ymz
  • 6,602
  • 1
  • 20
  • 39
0

With Regex:

var myRe = /<(.*)>/g;
var myArray = myRe.exec("____<asdf>___");
if (myArray)
    console.log(myArray[1]);

Regex test here

JSFiddle test here

maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37