前端tricks
发现一些值得研究的知识 先开个坑
https://blog.huli.tw/2022/04/14/javascript-string-regexp-magic/
挑战1
1 | var regexp = /huli/g |
test()方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回true或false。
看上去会完全一样的 结果却大跌眼镜
不看答案先思考一下为什么
感觉可能要看一下源码?
看一下文档
JavaScript
RegExpobjects are stateful when they have theglobalorstickyflags set (e.g./foo/gor/foo/y). They store alastIndexfrom the previous match. Using this internally,test()can be used to iterate over multiple matches in a string of text (with capture groups).
1 | const str = 'table football'; |
1 | > true |
interesting!
挑战2
1 | var password = prompt('input password') |
利用挑战1的stateful特性有可能吗?
怎么利用?用来爆破?差太多条件
要利用 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/input
Regex.input 函数
作者的答案 555 没好好打dicectf
1 | /hello/.test('hello world') |