-5

I have following string:

<a href="http://stackoverflow.com/ABC"> </a>

How can I extract text (i.e.ABC) from the string?

Teemu
  • 22,918
  • 7
  • 53
  • 106

2 Answers2

1

You can try anchorObject.pathname or anchorObject.href.match(/\/([^/]+)$/)[1] if you just want the last path.
You still need to get the anchorObject from the DOM.

const parser = new DOMParser();
const elm = parser.parseFromString('<a href="https://stackoverflow.com/posts"></a>', 'text/html');
const anchorObject = elm.getElementsByTagName('a')[0];

And voilà.

Fabien
  • 45
  • 5
0

If you are not sure of what the length of the variable you're looking for is, you can use this syntax.

let string = '<a href="http://stackoverflow.com/ABC"> </a>';
console.log(string.split('"')[1].split('/')[3]);

If you know what you're looking for ( for Example: ABC ) you can find index of ABC, and splice the string.

let variable = 'ABC'
console.log(string.substr(string.search("ABC"), variable.length))

Breakdown the string until you get the variable.

Aabhushan Gautam
  • 208
  • 1
  • 12