-4

i want to replace html links in a string with text links, for example:

<a href="test.com"> should become test.com.

I cant figure out any regex matching all my patterns. Cause links might have more attributes in different orders:

<a class="test" href="test.com" title="test">

How can i achieve that?

Barmar
  • 741,623
  • 53
  • 500
  • 612
fredalex
  • 433
  • 5
  • 15

2 Answers2

0

let str = '<a class="test" href="test.com" title="test">'
let result = str.split(/href="/)[1].split('"')[0]
console.log(result)
Ikechukwu Eze
  • 2,703
  • 1
  • 13
  • 18
0

Create a temporary DOM element with the string as HTML content and iterate over all a tags and replace it with the corresponding link(by getting href attribute).

let html = `<a class="test" href="test.com" title="test">`;

// create a temporary div element
let tempDiv = document.createElement('div');
// set html content as your string
tempDiv.innerHTML = html;

// get all a tags and iterate
tempDiv.querySelectorAll('a').forEach(ele => {
  // replace element with corresponding link
  ele.replaceWith(ele.getAttribute('href')) // or ele.href
})

// get html content of temporary element
console.log(tempDiv.innerHTML)

Or alternately you can use DOMParser for parsing html content.

let html = `<a class="test" href="test.com" title="test">`;

// parser
let parser = new DOMParser();
// parse the string which returs a document object
doc = parser.parseFromString(html, "text/html");


// get all a tags and iterate
doc.querySelectorAll('a').forEach(ele => {
  // replace element with corresponding link
  ele.replaceWith(ele.getAttribute('href')) // or ele.href
})

// get html content from body
console.log(doc.body.innerHTML)

UPDATE : With regex you can extract and replace the a tag in the following method(not prefered).

var str = '<a class="test" href="test.com" title="test">';

console.log(str.replace(/<a[^>]*href="([^"]+)"[^>]*>(?:.*?<\/a>)?/g, '$1'));


var str1 = '<a class="test" href="test.com" title="test">abc</a>';

console.log(str1.replace(/<a[^>]*href="([^"]+)"[^>]*>(?:.*?<\/a>)?/g, '$1'));

Reference : Using regular expressions to parse HTML: why not?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188