1

How to evaluate the valueof Javascript substring.

I have a string like that. I wanted the filter out value the value of my title.

myStr = "<span title='Country' ></span>"

What I am trying here is

myStr.search(/title/i);

here I am geting index of title. IS there any substring method where I can get the value of title.

David
  • 4,266
  • 8
  • 34
  • 69

1 Answers1

2

I would not match HTML with a reg exp. I would parse it to DOM.

const myStr = `<span title='Country' ><img src="classic/resources/images/country.png"/></span>`;

const parser = new DOMParser();
const doc = parser.parseFromString(myStr, "text/html");

console.log(doc.querySelector('span[title]').getAttribute('title'));
epascarello
  • 204,599
  • 20
  • 195
  • 236