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

正则表达式一张图总结 #15

Open
lq782655835 opened this issue Sep 7, 2018 · 0 comments
Open

正则表达式一张图总结 #15

lq782655835 opened this issue Sep 7, 2018 · 0 comments

Comments

@lq782655835
Copy link
Owner

lq782655835 commented Sep 7, 2018

正则表达式对于每个开发者都非常重要,用的好能在一些关键时刻让自己变得轻松。推荐个正则可视化工具:regulex,帮助学习者直观验证。

正则方法

regex.exec(string)

如果匹配成功,exec() 方法返回一个数组;匹配失败,返回 null

let regexExec = /#(.*)$/.exec('http://localhost:8081/#/demo')
/*
[ '#/demo',
'/demo',
index: 22,
input: 'http://localhost:8081/#/demo' ]
*/

regex.test(string)

查看正则表达式与指定的字符串是否匹配。返回 true 或 false

let regexTest = /#(.*)$/.test('http://localhost:8081/#/demo')
// true

string.match(regex)

类似regex.exec(string)

let stringMatch = 'http://localhost:8081/#/demo'.match(/#(.*)$/)
// [ '#/demo',
// '/demo',
// index: 22,
// input: 'http://localhost:8081/#/demo' ]

string.search(regex)

匹配成功,search() 返回正则表达式在字符串中首次匹配项的索引。否则,返回 -1。类似regex.test()

let stringSearch = 'http://localhost:8081/#/demo'.search(/#(.*)$/)
// 22

其他正则相关语法@jawil总结的十分详细,故转载在此:

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

1 participant