Skip to content

Instantly share code, notes, and snippets.

@vermaslal
Created November 25, 2015 11:32
Show Gist options
  • Save vermaslal/c4cb39c4c64ea45de767 to your computer and use it in GitHub Desktop.
Save vermaslal/c4cb39c4c64ea45de767 to your computer and use it in GitHub Desktop.
Replace href from anchor tag html using node js
var source = '<a href="double-quote test">double-quote test</a>\n' +
'<a href=\'single-quote test\'>single-quote test</a>\n' +
'<a href=\'single-quote test\'>single-quote test</a>\n' +
'<a class="foo" href="leading prop test">\n' +
'<a href="trailing prop test" class="foo">\n' +
'<a style="bar" link="baz" ' +
'name="quux" ' +
'href="multiple prop test" class="foo">\n' +
'<a class="foo"\n href="inline newline test"\n style="bar"\n >inline newline test</a>' +
'<a href=3D"with 3d data double quote"\n style="bar"\n >with 3d data double quote</a>' +
'<a href=3D\'with 3d data single quote\'\n style="bar"\n >with 3d data double quote</a>' +
'<link href="this is link" />';
var cnt = 0;
var updated = source.replace(/<a[^>]+?href=(?:3D)?('|")([^\1]+?)\1/gi, function (match, a1, url) {
cnt++;
// console.log(arguments)
return match.replace(url, cnt);
});
console.log(updated);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment