去除字符串左右两边的空格(实现代码)

网友投稿 199 2023-07-17


去除字符串左右两边的空格(实现代码)

在日常工作中,过滤表单中的一些特殊的字符是很常见的功能,比如文本中要求输入单纯的数字,但用户有时会误输入一些多余的空格或其他字符混合的文本,这显然不符合输入要求。

下面一起来学习怎么样去除字符串左右两边的空格。

过滤前:

过滤后:http://

HTML代码:

css代码:

html,body,div,input{margin:0;padding:0;}

.main{width:400px;height:auto;padding:0 15px;text-align:center;}

.main input{width:100%;height:35px;border:none;margin-top:20px;border-radius:5px;}

input[type="text"]{text-align:left;padding-left:15px;box-sizing:border-box;border:1px solid blue;}

input[type="button"]{width:50%;background:blue;}

@media only screen and (max-width: 415px) {

.main{width:100%;box-sizing:border-box;}

}

js部分:

var userName = document.getElementById('userName'),

rule = document.getElementById('rule'),

regexEmpty = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;

rule.onclick = function (){

userName.value = userName.value.pWUEYExzbOreplace(regexEmpty,''); //正则替换

console.log(userName.value);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:J2SE基础之在Eclipse中运行hello world
下一篇:20分钟轻松创建自己的Bootstrap站点
相关文章

 发表评论

暂时没有评论,来抢沙发吧~