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
  • 3,817
  • 8
  • 26
  • 54

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
  • 195,511
  • 20
  • 184
  • 225