JavaScript正则表达式问题?

查看:103
本文介绍了JavaScript正则表达式问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

有这样一个正则表达式 /.(w+)/g,exec一个字符串'a.b.c',结果是什么?不是得到.b和.c吗?

/\.(\w+)/g.exec('a.b.c')

解决方案

var reg = /\.(\w+)/g
undefined
var str = 'a.b.c';
undefined
reg.exec(str)
[".b", "b"]
reg.exec(str)
[".c", "c"]
reg.exec(str)
null


Return value

If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

If the match fails, the exec() method returns null.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

这篇关于JavaScript正则表达式问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆