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")
}

“`

    原文作者:小渝人儿
    原文地址: https://segmentfault.com/a/1190000000376455
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞