Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则g和i的区别 #69

Closed
1078559858 opened this issue Nov 27, 2019 · 1 comment
Closed

正则g和i的区别 #69

1078559858 opened this issue Nov 27, 2019 · 1 comment

Comments

@1078559858
Copy link

关于这一点应该写仔细点。
g一般用于exec、match、replace,如果用于test可能会导致问题。
因为g模式会生成一个lastindex参数来存储匹配最后一次的问题,可以设置lastindx为0.

var str = 'asdf123'
var str2 = 'asdf123'
var reg = new RegExp('s','g')
var res = reg.test(str)
reg.lastIndex = 0; // 记录上一次匹配的位置
var res2 = reg.test(str2)
console.log(res);
console.log(res2)
@qianguyihao
Copy link
Owner

感谢指正,已补充。

qianguyihao added a commit that referenced this issue Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants