有人可以帮我解决这些正则表达式吗?
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";
}
非常感谢!
答案 0 :(得分:0)
.replace(/(^\s*)|(\s*$)/gi,"")
如果
^
任意*
空格\s
或|
,如果任何*
空格\s
一直到最后{{}} {1}}将其替换为$
。
""
至少两个或更多
.replace(/[ ]{2,}/gi," ")
个连续空格{2,}
,替换为一个空格[ ]
。
" "
换行符
.replace(/\n /,"\n")
和空格\n
,仅替换换行符。这就像是EOL的修剪。
因此,简而言之,这些正则表达式正在清理任何草率的白色空间,以确保准确的字数。