2
有人可以幫我用這些正則表達式嗎?這些正則表達式如何計算HTML中的單詞?
wordCountTemp = wordCountTemp.replace(/(^\s*)|(\s*$)/gi,"");
wordCountTemp = wordCountTemp.replace(/[ ]{2,}/gi," ");
wordCountTemp = wordCountTemp.replace(/\n /,"\n");
我不明白他們是如何工作的。以下是完整的代碼。
function() {
var wordCountTemp = $("#content").text();
wordCountTemp = wordCountTemp.replace(/(^\s*)|(\s*$)/gi,"");
wordCountTemp = wordCountTemp.replace(/[ ]{2,}/gi," ");
wordCountTemp = wordCountTemp.replace(/\n /,"\n");
var wordCount = wordCountTemp.split(' ').length;
if (wordCount>3000) return "3k+";
else if (wordCount>2500) return "2.5k-3k";
else if (wordCount>2000) return "2k-2.5k";
else if (wordCount>1500) return "1.5k-2k";
else if (wordCount>1100) return "1.1k-1.5k";
else if (wordCount>800) return "800-1.1k";
else if (wordCount>500) return "500-800";
else if (wordCount>200) return "200-500";
else return "<200";
}
非常感謝!
什麼是問題,你的目標是什麼,你碰到什麼問題,你得到了什麼結果? – Hodrobond
@ Toni2708您可能會覺得這有幫助http://eloquentjavascript.net/09_regexp.html – mrbubbles
嗨@Hodrobond我只是想找出什麼是匹配的/(^ \ s *)|(\ s * $)/ gi,/ [] {2,}/gi或/ \ n /,「\ n」。感謝指導@ mrbubbles!也許你們可以告訴我我是如何得到這個div.mk-main-wrapper-holder:nth-child(3)> div:nth-child(1)> div:nth-child(1) var wordCountTemp = $(「#content」)。text();線。謝謝 – Toni2708