Quick actions

cmd+k|ctrl+k

Navigation

Languages

Replace Anchor Text with Angular NgClick

Snippet info

Language

JavaScript

Visibility

public

Author

mirulumam

Created

2018-09-18T09:46:55Z

Updated

2018-09-18T09:56:14Z

var string = "Thanks a lot <a href='https://glot.io'>glot.io</a>. You're awesome. Thanks a lot <a href=\"https://glot.io\">glot.io</a>. You're awesome. Thanks a lot <a href='glot.io'>glot.io</a>. You're awesome";
var regex = /<a([^>]*?)href\s*=\s*(['"])([^\2]*?)\2\1*>([^>]*?)<\/a>/gmi; // thanks to https://stackoverflow.com/a/40887021/4324393

var replace = string.replace(regex, function(full, group1, group2, group3, group4) { // thanks to https://stackoverflow.com/a/21711130/4324393
    var result = full.replace(full, "<span ng-click=\"openLink('" + group3 + "')\">" + group4 + "</span>");
    return result;
});

console.log(replace);

INFO