我是一个 小可爱 是不是压?
我想用正则表达式去掉第二个空格但是保留第一个空格能做么?
(?<=\s\S*)\s
对于“关于正则 表达式的问题去掉空格”的问题有以下四种解决方案:
1.把左边空格去掉.replace(/^\s*/g,"");
2.把右边空格去掉.replace(/\s*$/g,"");
3.把前后空格去掉.replace(/(^\s*)|(\s*$)/g,"")
4.把所有的空格去掉.replace(/\s+/g,"")
空格测试:
<script>
$(function(){
var test =$('#test').val();
alert(test);
})
function blank(){
var test =$('#test').val();
<%--去掉左边的空格 --%>
alert("a"+test);
test = test.replace(/^\s*/g,"");
alert("去掉左边的空格"+test);
<%--去掉右边的空格 --%>
alert(test+"a");
test = test.replace(/\s*$/g,"");
alert(test+"去掉右边的空格");
<%--去掉字符串前后的空格--%>
test = test.replace(/(^\s*)|(\s*$)/g, "");
alert("去掉字符串前后的空格"+test+"a");
<%--去掉字符串中的所有空格--%>
test = test.replace(/\s+/g,"");
alert("a"+test+"a");
}
</script>
以上四种方案,择优采纳!