`
ygsilence
  • 浏览: 332645 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

js match 的简单用法 [转]

    博客分类:
  • JS
阅读更多

/* js match 的用法 */
var href,rs,domain
href = 'http://www.baidu.com/test/index.php?id=3&a=5';
rs = href.match(/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/); //返回数组
/*
for (str in rs) {
    document.writeln('索引' + str + '的值是:' + rs[str] + "<br>" );
}
*/
//上面的输出结果如下;
/*
索引0的值是:http://www.baidu.com/test/index.php?id=3&a=5
索引1的值是:http
索引2的值是:www.baidu.com
索引3的值是:undefined
索引4的值是:/test/index.php?id=3&a=5
索引index的值是:0
索引input的值是:http://www.baidu.com/test/index.php?id=3&a=5
*/
domain = RegExp.$2; //$1,$2,$3 ... 分别代表第一个括号,第二个括号,...值
alert(domain);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics