Reputation:
This is my body:
<div>http://www.example.com</div>
<span>http://www.mywebsite.com</span><br>
<b>http://www.domain.com</b>
This is my simple regex (works):
document.body.innerHTML.match(/(http|https|ftp):\/\/(?:www\.)?([a-z]|\d|-)+\.(?:([a-z]|-)+\.)*?(com|net|org)(?:\/)?/ig);
How can I add a class to all elements that contain the matches in javascript or jquery? Example:
<div class="matchClass">http://www.example.com</div>
<span class="matchClass">http://www.mywebsite.com</span><br>
<b class="matchClass">http://www.domain.com</b>
Upvotes: 1
Views: 324
Reputation: 1
Try
$.each(myMatch, function(k, v) {
$("body *:contains(" + v + ")").addClass("matchClass")
})
jsfiddle http://jsfiddle.net/guest271314/cz6owdxx/3/
Upvotes: 1
Reputation: 67968
(>(http|https|ftp):\/\/(?:www\.)?([a-z]|\d|-)+\.(?:([a-z]|-)+\.)*?(com|net|org)(?:\/)?)
You can try this.Repalce by class="matchClass"$1
.
See demo.
http://regex101.com/r/hQ1rP0/16
Upvotes: 0
Reputation: 59252
Use this regex
/<(.+)>((http|https|ftp):\/\/(?:www\.)?([a-z]|\d|-)+\.(?:([a-z]|-)+\.)*?(com|net|org)(?:\/)?)<\/(.+)>/ig
And replace with
<$1 class="matchClass">$2<$7>
Upvotes: 1