<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script> window.onload = function() { var oText = document.getElementById("text1"); var oBtn = document.getElementById("btn1"); oBtn.onclick = function(){ alert(oText.value.length) oText.value = oText.value.replace(/(^\s*)|(\s*$)/g, ""); alert( oText.value.length) } } </script> </head> <body> <input id="text1" type="text" value=""> <input id="btn1" type="button" value="过滤掉空格"> </body> </html> //删除左右两端的空格 return str.replace(/(^\s*)|(\s*$)/g, ""); // 删除左边的空格 return str.replace(/(^\s*)/g,""); //删除右边的空格 return str.replace(/(\s*$)/g,"");
javascript去除字符串左右两端的空格
最新推荐文章于 2020-05-07 10:09:03 发布