我一直在尋找,我發現這個Question堆棧溢出 所以我一直在努力使它與我的代碼工作,我似乎無法得到它工作。查找和動態替換文本Javascript(不區分大小寫)
html = '<li style="list-style:none;">';
cmnt = this.comment.replace(new RegExp("(" + preg_quote(firsttext) + ")" , 'gi'), "<span class='cutecl'>$1</span>");
cmnt = cmnt.replace(new RegExp("(" + preg_quote(secondtext) + ")" , 'gi'), "<span class='wincl'>$1</span>");
cmnt = cmnt.replace(new RegExp("(" + preg_quote(thirdtext) + ")" , 'gi'), "<span class='failcl'>$1</span>");
html += cmnt;
html += '<br/><a href="http://www.youtube.com/userPage.php?author='+escape(this.author)+'">'+this.author+'</a>';
html += '<span class="label"> - '+(this.published.getMonth() + 1)+'/'+this.published.getDate()+'/'+this.published.getFullYear()+'</span>';
html += '</li>';
$('#comment').append(html);
,當然還有:
function preg_quote(str) {
return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
}
時,我有
cmnt = this.comment.replace(new RegExp("(" + preg_quote(firsttext) + ")" , 'gi'), "<span class='cutecl'>$1</span>");
cmnt = cmnt.replace(new RegExp("(" + preg_quote(secondtext) + ")" , 'gi'), "<span class='wincl'>$1</span>");
cmnt = cmnt.replace(new RegExp("(" + preg_quote(thirdtext) + ")" , 'gi'), "<span class='failcl'>$1</span>");
包括在我的代碼什麼時,我想它來創建,李出現,但如果它走了,它工作正常,任何線索我做錯了什麼?在此先感謝,阿里
這是問題的一個小提琴:http://jsfiddle.net/Yg8Qe/2/
順便說一句我思念的東西或不使用JS'\ 1'其匹配參考,不'$ 1'? – 2011-03-16 23:46:35
@Brad Christie這完全有可能,我只是使用從其他問題收集的信息:p – Ali 2011-03-17 01:30:19
JavaScript是$ – epascarello 2011-03-17 04:19:09