i need to add a ng-click attribute to a IMG tag present in a string, using String.replace.
Example:
<p>Test1</p>
<p>
<img class="fr-dib fr-draggable" src="http://admin.fsn.dev/uploads/a4cafaf13aabb97b041a50ae55c7bb6d331a7cc4.jpg">
Foto1
</p>
<p>
<img class="fr-dib fr-draggable" src="http://admin.fsn.dev/uploads/c5a7fc84d8f552d208e5ee0e0cbcecc0d547b441.jpg" style="width: 300px;">
</p>
Needs to be converted to:
<p>Test1</p>
<p>
<img class="fr-dib fr-draggable" src="http://admin.fsn.dev/uploads/a4cafaf13aabb97b041a50ae55c7bb6d331a7cc4.jpg" ng-click="openModal('http://admin.fsn.dev/uploads/a4cafaf13aabb97b041a50ae55c7bb6d331a7cc4.jpg')">
Foto1
</p>
<p>
<img class="fr-dib fr-draggable" src="http://admin.fsn.dev/uploads/c5a7fc84d8f552d208e5ee0e0cbcecc0d547b441.jpg" ng-click="openModal('http://admin.fsn.dev/uploads/c5a7fc84d8f552d208e5ee0e0cbcecc0d547b441.jpg')" style="width: 300px;">
</p>
Any suggestion?
I tried with this but no luck :(
data = data.replace(/<img(.*)src="(.*)"(.*)>/ig,'<img $1 src="$2" ng-click="openModal(\'$2\')" $3')
Try this:
data.replace(/(<img.*src="([^"]*)"[^>]*)>/ig,'$1 ng-click=\"openModal(\'$2\')\">');
The regex only uses 2 capture groups.
Although I doubt that regex is the ideal way to add attributes to a tag.
For example .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.