去除字符串左右两边的空格
<html>
<head>
<title>2.1节,去除字符串左右两边的空格</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>测试去除字符串左右两边的空格</h2>
<input type='text' id='strs' value=' 需要过滤空格 '>
<input type='button' id='rstrsBtn' value=' 过滤 '>
<script type="text/javascript">
window.onload = function(){
var _rstrsBtn = document.getElementById("rstrsBtn"), //获取过滤按钮对象
_strs = document.getElementById("strs"); //获取被过滤元素
_rstrsBtn. onclick = function(){//去除空格
_strs.value = _strs.value.replace( /^(\s|\u00A0) |(\s|\u00A0) $/g, "" );//正则替换
}
};
</script>
</body>
</html>
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved