<head>
        
<title>Trim Example</title>
        
<script type="text/javascript">
                String.prototype.trim 
= function () {
                    
//使用.*?量词忽略优先
                    var reExtraSpace = /^\s*(.*?)\s*$/;
                    
return this.replace(reExtraSpace, "$1");
                };

                
                
function showText() {
                    
var oInput1 = document.getElementById("txt1");
                    
var oInput2 = document.getElementById("txt2");
                    oInput2.value 
= oInput1.value.trim();
                }
        
</script>
    
</head>
    
<body>
        
<p>Enter some text containing leading and trailing spaces below and click the button<br />
        
<textarea rows="10" cols="50" id="txt1"></textarea><br />
        
<input type="button" value="Trim" onclick="showText()" /></p>
        
<p>Trimmed Text:<br />
        
<textarea rows="10" cols="50" id="txt2"></textarea></p>
        
    
</body>
</html>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-15
  • 2021-08-13
  • 2021-12-20
相关资源
相似解决方案