Skip to content Skip to sidebar Skip to footer

Regex To Remove Html Tags With Certain Class From String

I need to remove all a tags which have a certain class from a string and store them in another string. For example: var str = 'this is a string link&

Solution 1:

var re = /(<a(?: \w+="[^"]+")* class="link"(?: \w+="[^"]+")*>([^<]*)<\/a>)/g;
var str = 'this is a string <a href="#" class="link">link</a> <a class="link">link2</a>';

var links = [] # array of <a> tags
for (var i in str.match(re)) {
    links.push(str.match(re)[i])
}

var embedded_strings = [] # array of strings inside <a> tags
for (var i in links) {
    embedded_strings.push(links[i].replace(re, "$2"))
}

Result:

links = ['<a href="#" class="link">link</a>', '<a class="link">link2</a>']
embedded_strings = ['link', 'link2']

This answer assumes that there will be no whitespace around = and that you will use double quotes exclusively.


Post a Comment for "Regex To Remove Html Tags With Certain Class From String"