如何使用JQuery在HTML中查找文本?

有没有办法对某些HTML进行搜索,每当发现正则表达式时,将其包装在一个范围内?

例如:

The rain in Spain stays mainly on the plain

$('p').match('/ain/gi').each(function() { $(this).wrap(''); });

我强烈建议采用以下更简单的方法:

 // selects the 'p' elements, and calls the 'html()' method: $('p').html(function (i,h) { // uses the anonymous function, i is the index of the current element // from the collection, h is the found HTML of that element. return h.replace(/(ain)/gi, '$1'); }); 

您可以使用正则表达式替换:

 $('p').html($('p').text().replace(/(ain)/gi, '$1')); 

http://jsfiddle.net/hus​​gns7t/