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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞