-5

I have following string:

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

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

3

2 Answers 2

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à.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm able to fetch the string document.getElementsByTagName('a')[19].innerHTML;
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.