trim()、replace()、search()
<script> //trim();去除前后的空格 var str = " 你好 我很好! "; console.log(str); console.log(str.trim()); console.log("--------------------"); //replace(); 替换和全局替换值。 var str = "Today is fine day,today is fine day!" console.log(str); console.log(str.replace(/today/ig, "tomorrow"));//i忽略大小写,g代表全局 console.log("--------------------"); //search(); 给字符差索引 var str = "abcdefg"; console.log(str.search(/bc/)); console.log(str.indexOf("bc")); </script>
trim()IE678的兼容写法
<script> var str = " 你好,我 很好! "; console.log(str); console.log(trim(str)); function trim(str) { var aaa = str.replace(/(^s+)|(s+$)/g, ""); return aaa; } </script>