Created
February 9, 2010 09:32
-
-
Save kotarok/299050 to your computer and use it in GitHub Desktop.
mark part of string with given jQuery element by RegExp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param query {RegExp} required | |
* @param wrapElement {jQuery} required | |
**/ | |
$.fn.markTextByRegExp = function(query,wrapElement){ | |
var REtag = '<\\/?[^>]+?\\/?>'; | |
var REtagDivider = new RegExp(REtag+'|[^<>]*','gi'); | |
return this.each(function(){ | |
var elem = $(this); | |
var srcs = elem.html().match(REtagDivider); | |
for(var i=0,l=srcs.length;i<l;i++){ | |
var obj = srcs[i]; | |
if(!RegExp(REtag).test(obj)){ | |
srcs[i] = obj.replace(query,function(matched){ | |
return $('<div />').append(wrapElement.html(matched)).html(); | |
}) | |
} | |
} | |
elem.html(srcs.join('')); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment