删除换行符javascript问题
嗨,我需要从 javascript 中的字符串中删除所有空格和换行符...这是我使用的,但它不起作用...
<script language="JavaScript" type="text/javascript">
<!--
var isShift=null;
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var OP = (navigator.appName.indexOf("Opera")!=-1);
if(OP)isNN=true;
var key;
function shift(event){
key = (isNN) ? event.which : event.keyCode;
if (key==16)isShift=1;
}
function process(event){
key = (isNN) ? event.which : event.keyCode;
if(document.layers&&event.modifiers==4){
isShift=1;
}
if (key==13&&isShift!=1){
var chatmsg = $("#chatmsg");
var cmessage = chatmsg.val();
cmessage = cmessage.replace(/\s/g, '');
if(cmessage=="")
{
document.myForm.chatmsg.value = "";
return false;
}
else
{
sendMsg();
}
}
if (key!=16)isShift=null;
}
//-->
function sendMsg(){
var chatmsg = $("#chatmsg");
$.post("sendchat.php",{ chatmsg: chatmsg.val()} ,
function(data)
{
$("#msgtousr").html(data).show();
$("#msgtousr").html(data).fadeOut(2000);
document.myForm.chatmsg.value = "";
});
}
</script>
<form name='myForm' method='POST' style='margin:0px'>
<textarea name='chatmsg' onkeypress='return process(event)' onkeydown='shift(event)' style='width:278px;height:70px;border:2px solid white;background-color:turquoise;color:blue' id='chatmsg'></textarea>
</form>
这是我的聊天记录,如果它有错误检查用户是否在 javascript 中输入了某些内容我怀疑替换功能行为不当
Hi i need to strip all spaces and newlines from a string in javascript... This is what i use and it doesn't work...
<script language="JavaScript" type="text/javascript">
<!--
var isShift=null;
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var OP = (navigator.appName.indexOf("Opera")!=-1);
if(OP)isNN=true;
var key;
function shift(event){
key = (isNN) ? event.which : event.keyCode;
if (key==16)isShift=1;
}
function process(event){
key = (isNN) ? event.which : event.keyCode;
if(document.layers&&event.modifiers==4){
isShift=1;
}
if (key==13&&isShift!=1){
var chatmsg = $("#chatmsg");
var cmessage = chatmsg.val();
cmessage = cmessage.replace(/\s/g, '');
if(cmessage=="")
{
document.myForm.chatmsg.value = "";
return false;
}
else
{
sendMsg();
}
}
if (key!=16)isShift=null;
}
//-->
function sendMsg(){
var chatmsg = $("#chatmsg");
$.post("sendchat.php",{ chatmsg: chatmsg.val()} ,
function(data)
{
$("#msgtousr").html(data).show();
$("#msgtousr").html(data).fadeOut(2000);
document.myForm.chatmsg.value = "";
});
}
</script>
<form name='myForm' method='POST' style='margin:0px'>
<textarea name='chatmsg' onkeypress='return process(event)' onkeydown='shift(event)' style='width:278px;height:70px;border:2px solid white;background-color:turquoise;color:blue' id='chatmsg'></textarea>
</form>
This is my chat if it has an error checking if the user typed something in in the javascript i suspect the replace function to misbehave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想删除所有空格和换行符吗?只需要一个正则表达式:
You want to strip all spaces and newlines? Only one regex is necessary:
尝试...
cmessage = cmessage.replace(/\s/g, '');
\s
在这种情况下代表“所有空白”,其中包括空格和换行符。参考
Try...
cmessage = cmessage.replace(/\s/g, '');
\s
in this case stands for "all white space" which includes spaces and newline characters.Reference