var testString=" this is the string "; alert(testString); testString=testString.trim(); //空白去掉了 if(typeof String.trim=="undefined"){ String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g,""); } } // ^ 匹配輸入的開始 // \s 匹配一個單個的空白字符 // * 匹配0次或屢次 // $ 匹配輸入的結束 // g 對整個字符串都使用該匹配 alert(testString);
var a = "Chrome/test"; // Chrome/的非空字符 if (/Chrome\/(\S+)/.test(a)) { console.log("a"); } else { console.log("b") }
```javascript