我对正则表达式有一个非常奇怪的问题。这是我的表达:
const csrfRegex = /"csrf_token": "((\\"|[^"])*)"/ig;
这样做的目的是从已删除站点上的Javascript对象中提取CSRF令牌。页面摘录如下:
[...]
<script type="text/javascript">window._sharedData = { "config": {"viewer": null, "csrf_token": "rYzP3AWdRFIoz9UJbyholRAX5QSiirCR"}, "display_properties_server_guess": {"viewport_width": 360, "pixel_ratio": 1.5}, "entry_data": {"LoginAndSignupPage": [{}]}, "platform": "web", "qe": {"discovery": {"p": {}, "g": ""}, "us": {"p": {"use_continue_text": "false"}, "g": "continue_vs_signup_text_control_03"}, "ebd": {"p": {}, "g": ""}, "gql": {"p": {}, "g": ""}, "freq": {"p": {}, "g": ""}, "br": {"p": {}, "g": ""}, "feed": {"p": {}, "g": ""}, "su_universe": {"p": {}, "g": ""}, "profile": {"p": {}, "g": ""}, "us_li": {"p": {}, "g": ""}}, "language_code": "en", "environment_switcher_visible_server_guess": true};</script>
[...]
如果我使用Regex101在线测试,第一个捕获组会正确匹配:https://regex101.com/r/zDxPFw/1
但是,在我的代码中,它无法找到匹配项,并在if语句中返回:
let csrf_scrape = csrfRegex.exec(body);
// Both must have a length of at least 2
if (!csrf_scrape || !csrf_scrape.length) {
console.log(body);
return next(`No CSRF token found`);
}
let csrf = csrf_scrape[1];
return next(null, csrf);
知道为什么会这样吗?
答案 0 :(得分:0)
首先,我不认为你需要内心的群体,所以这应该足够我想象了:
var csrfRegex = /"csrf_token": "((?:\\"|[^"])*)"/i;
使用(?:)
是非捕获组。然后你可以这样轻松地得到它:
var value = text.match(csrfRegex)[1];
g
必须删除str.match()
标记才能返回组值。