正则,防止用户恶意输入大量回车2

qfljm 2007-08-08 03:39:59
1.替换字符串所有连续2个以上的空行为一个空行.
2.接着替换所有行中行字符在5个以下,行连续3行以上的部分中的回车为空格

如字符串(以下\n是回车符):
a\n
\n
\n
\n
bbbbbbbbbbbbbb\n
aaa\n
aaa\n
aaa\n
aaa\n
处理后应该是
a\n
\n
bbbbbbbbbbbb\n
aaa aaa aaa aaa
...全文
189 5 点赞 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yifuzhiming 2007-08-08
限制一下输入长度不OK 了
  • 打赏
  • 举报
回复
hbhbhbhbhb1021 2007-08-08
我下面这个代码可能有漏洞,只是给你个正则的思路.你多测测再改改吧!
<textarea id="textarea1" style="width:400;height:400">
a



bbbbbbbbbbbbbb
aaa
aaa
aaa
aaa
</textarea>
<input type="button" onclick=check()>
<script language=javascript>
function check(){
re=/(\r\n(?=(\r\n+)))+/g
var obj=document.getElementById("textarea1")
var str=obj.value
str=str.replace(re,"\r\n");
re=/\r\n((([^\r\n]{1,5})(\r\n|$)){3,})/g
alert(str.match(re))
obj.value=str.replace(re,function(a,b,c){return "\r\n"+b.split("\r\n").join(" ")+"\r\n"})
}

</script>
  • 打赏
  • 举报
回复
dexterfar 2007-08-08
这样就ok了。
<textarea cols="30" rows="20" id="tr_from">
a



bbbbbbbbbbbbbb

aaa
aaa
aaa
aaa
</textarea>

<div><button onclick="Convert();">Convert</button></div>
<textarea cols="30" rows="20" id="tr_to"></textarea>
<script language="javascript">
var switchKey = true;
var dst_str="";
var blankCount = 0;
var shortLineCount = 0;
var shortLineArr = new Array();
function Convert()
{
var f_str = document.getElementById("tr_from").value;
f_str.replace(/^([\d\D]*?)$/gm,f1);
document.getElementById("tr_to").value = dst_str;
switchKey = true;
dst_str="";
blankCount = 0;
shortLineCount = 0;
shortLineArr = new Array();
}
function f1(a,b)
{
if(!switchKey)
{
switchKey = !switchKey;
return;
}
switchKey = !switchKey;
//alert(b.length);
if (/^\s*$/.test(b))
{
if(shortLineArr.length > 2)
{
dst_str = dst_str + shortLineArr.join(" ") + "\n";
shortLineArr = null;
shortLineArr = new Array();
}
else if(shortLineArr.length > 0)
{

dst_str = dst_str + shortLineArr.join("\n") + "\n";
shortLineArr = null;
shortLineArr = new Array();
}

blankCount++;
}
else if(b.length < 5 )
{
if(blankCount < 3 && blankCount > 0)
{
dst_str = dst_str+"\n";
blankCount = 0;
}
else if(blankCount >= 3)
{

dst_str = dst_str+"\n";
blankCount = 0;
}
shortLineArr.push(b);
shortLineCount++;
}
else
{

if(blankCount < 3 && blankCount > 0)
{
dst_str = dst_str+"\n";
blankCount = 0;
}
else if(blankCount >= 3)
{

dst_str = dst_str+"\n";
blankCount = 0;
}
shortLineCount=0;
blankCount=0;
if(shortLineArr.length > 2)
{
dst_str = dst_str + shortLineArr.join(" ") + "\n";
shortLineArr = null;
shortLineArr = new Array();
}
else if(shortLineArr.length > 0)
{
dst_str = dst_str + shortLineArr.join("\n") + "\n";
shortLineArr = null;
shortLineArr = new Array();
}
}

if(!(/^\s*$/.test(b) || b.length < 5))
{
dst_str = dst_str + b + "\n";
}

}
</script>
  • 打赏
  • 举报
回复
qfljm 2007-08-08
没处理第二种情况啊
  • 打赏
  • 举报
回复
btbtd 2007-08-08
<script type="text/javascript">
//<![CDATA[
var str =
[
'a\n'
,'\n'
,'b\n'
,'\n'
,'c\n'
,'\n'
,'\n'
,'\n'
,'d\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'e\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'f\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'g\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'\n'
,'h\n'
].join("");

document.write("str: \n", str, "<hr/>");

document.write("fClearLine(str, 0): \n", fClearLine(str, 0), "<hr/>");
document.write("fClearLine(str, 1): \n", fClearLine(str, 1), "<hr/>");
document.write("fClearLine(str, 2): \n", fClearLine(str, 2), "<hr/>");
document.write("fClearLine(str, 3): \n", fClearLine(str, 3), "<hr/>");
document.write("fClearLine(str, 4): \n", fClearLine(str, 4), "<hr/>");
document.write("fClearLine(str, 5): \n", fClearLine(str, 5), "<hr/>");
document.write("fClearLine(str, 6): \n", fClearLine(str, 6), "<hr/>");

function fClearLine(Source, iProtect, bDebug)
{// shawl.qiu code : return string || void return
var bStr = false;
if(!iProtect) iProtect = 0;
if(Source.constructor==String) Source = [Source], bStr = true;

var sPtr = "[\\s]*?\\n";
var sFianlPtr = fRepeatString(sPtr, iProtect);
var TempAr = ["(\\n", sFianlPtr, ")", "[\\s]*\\n"];
var TempRe = new RegExp(TempAr.join(""), "g");

Source[0] = Source[0].replace(TempRe, "$1");

if(bDebug)
{
document.write("sFianlPtr: ", sFianlPtr, "<br/>");
}

if(bStr) return Source[0];

function fRepeatString(sIpt, iLen)
{// shawl.qiu script
if(!sIpt)return "";
if(!iLen&&iLen!==0) iLen = 2;
iLen -= 0;
return new Array(iLen+1).join(sIpt);
} // end function fRepeatStr
} // end function fClearLine(Source, iProtect, bDebug)
//]]>
</script>
  • 打赏
  • 举报
回复
相关推荐
发帖
JavaScript
加入

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2007-08-08 03:39
社区公告
暂无公告