Home > Blockchain >  How to find the exact value of href in <a> tag using regex
How to find the exact value of href in <a> tag using regex

Time:08-24

I have a string that contains an <a> tag with href attributes. I need to find the regex which matches only value of hrefs.

<a href="http://value.com">VALUE HERE</a> <-- string to find
<a href="www.twittor.com">TWITTOR VALUE HERE</a> <-- another string to find

I would like to get exact http://value.com or www.twittor.com. I searched the site for an answer, many solutions were found, but they all match additional information, not the value itself.

Like this one: Regex to find Href value matches href="http://value.com" and so the others.

CodePudding user response:

Use a regular expression with a capturing group (enclosed in ()). Then use .exec and grab the last item from the return value of .exec:

const inputA = '<a href="http://value.com">VALUE HERE</a>';
const inputB = '<a href="www.twittor.com">TWITTOR VALUE HERE</a>';



const last = list => list[list.length - 1];
const extract = input => /href="(.*)"/g.exec(input);

console.log(last(extract(inputA)));
console.log(last(extract(inputB)));

CodePudding user response:

Using the native DOM parser might be a viable alternative to a regex. Pass in the string, parseFromString, and then return the href attribute of the first child element in the body of the document returned by the parser.

const str1 = '<a href="http://value.com">VALUE HERE</a>';
const str2 = '<a href="www.twittor.com">TWITTOR VALUE HERE</a>';

const parser = new DOMParser();

function getHref(parser, str) {
  return parser
    .parseFromString(str, 'text/html')
    .body.firstChild.getAttribute('href');
}

console.log(getHref(parser, str1));
console.log(getHref(parser, str2));

  • Related