Javascript正則表達式實例

javascript使用正則表達式來去除字符串空白

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);

Chrome/的非空字符

var a = "Chrome/test"; // Chrome/的非空字符
if (/Chrome\/(\S+)/.test(a)) {
console.log("a");
} else {
 console.log("b")
}

```javascript

相關文章
相關標籤/搜索