JavaScript清除空格、换行,把双引号转换成单引号
1、页面
2、源码
1 <!DOCTYPE> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>清除字符串的空格和双引号</title> 6 <style type="text/css"> 7 textarea{ 8 padding:10px; 9 font-size:18px; 10 width:100%; 11 resize:none; 12 } 13 .main{ 14 padding:40px 10px; 15 clear:both; 16 overflow:hidden; 17 } 18 19 .main .left{ 20 float:left; 21 width:40%; 22 } 23 .main .center{ 24 25 float:left; 26 width:20%; 27 28 text-align:center; 29 30 } 31 .main .right{ 32 float:right; 33 width:40%; 34 } 35 .footer{ 36 text-align:center; 37 color:red; 38 } 39 </style> 40 </head> 41 <body> 42 <div class="main"> 43 <div class="left"> 44 <textarea rows="10" id="ta1"></textarea> 45 </div> 46 47 <!--【中部开始】--> 48 <div class="center"> 49 <br> 50 <label for="ck1">(1) 清 除 空 格</label><input type="checkbox" id="ck1" checked="checked"><br><br> 51 <label for="ck2">(2) 清 除 换 行</label><input type="checkbox" id="ck2" checked="checked"><br><br> 52 <label for="ck3">(3) 双引号转单引号</label><input type="checkbox" id="ck3" checked="checked"><br> 53 <br> 54 <button onclick="Run()">转换</button> 55 </div> 56 <!--【中部结束】--> 57 58 <div class="right"> 59 <textarea rows="10" id="ta2"></textarea> 60 </div> 61 </div> 62 <div class="footer">提示:如果包含有HTML标签,建议不要选择清除空格!</div> 63 64 <script type="text/javascript"> 65 function Run(){ 66 var ta1 = document.getElementById("ta1"); 67 var ta2 = document.getElementById("ta2"); 68 var ck1 = document.getElementById("ck1"); 69 var ck2 = document.getElementById("ck2"); 70 var ck3 = document.getElementById("ck3"); 71 var rst = ta1.value; 72 73 if(ck1.checked){ 74 //alert(1); 75 rst = Trim(rst); 76 } 77 78 if(ck2.checked){ 79 rst = ClearBr(rst); 80 } 81 82 if(ck3.checked){ 83 rst = Change(rst); 84 } 85 //rst += "+++"; 86 ta2.value = rst; 87 ta2.select(); 88 document.execCommand("Copy"); // 执行浏览器复制命令 89 alert("执行完成,可以直接粘贴"); 90 91 } 92 93 //去除空格 94 function Trim(str){ 95 return str.replace(/\s+/g,""); 96 } 97 98 //去除换行 99 function ClearBr(str) { 100 //str = str.replace(/<\/?.+?>/g,""); 101 str = str.replace(/[\r\n]/g, ""); 102 return str; 103 } 104 function Change(str){ 105 return str.replace(/\"/g, "'"); 106 } 107 </script> 108 </body> 109 </html>
生命不息,学习不止