正则表达式一张图总结
Opened this issue · 0 comments
lq782655835 commented
正则表达式对于每个开发者都非常重要,用的好能在一些关键时刻让自己变得轻松。推荐个正则可视化工具: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总结的十分详细,故转载在此: