可以使用正则表达式来匹配字符串中的userId值:
var str = `<!DOCTYPE html>
<html>
<head>
<title>admin</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/files/ab6f43102ee4085c488ffc92dea4b5d5/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="/themes/core/static/css/fonts.min.css?d=5fc27c99">
<link rel="stylesheet" href="/themes/core/static/css/main.min.css?d=5fc27c99">
<link rel="stylesheet" href="/themes/core/static/css/core.min.css?d=5fc27c99">
<script type="text/javascript">
var init = {
'urlRoot': "",
'csrfNonce': "ecb75371c8c39d77c883798622cb32001516157540afe1c688fd7dc1dd7ad77e",
'userMode': "users",
'userId': 28,
'start': 1677553860,
'end': null,
'theme_settings': null
}
</script>
</head>
<body>`;
var regex = /'userId'\s*:\s*(\d+)/;
var match = regex.exec(str);
if (match) {
console.log(match[1]); // 输出:28
} else {
console.log("未找到userId");
}
以上代码会输出28
,表示已成功匹配到了字符串中的userId值。