Oba*_*bay 5 javascript regex node.js
假设我在字符串中有这个HTML:
<meta http-equiv="Set-Cookie" content="COOKIE1_VALUE_HERE">
<meta http-equiv="Set-Cookie" content="COOKIE2_VALUE_HERE">
<meta http-equiv="Set-Cookie" content="COOKIE3_VALUE_HERE">
Run Code Online (Sandbox Code Playgroud)
我有这个正则表达式,以获取content属性内的值:
/<meta http-equiv=[\"']?set-cookie[\"']? content=[\"'](.*)[\"'].*>/ig
Run Code Online (Sandbox Code Playgroud)
如何使用JavaScript获取所有三个content值?
我试过了:
var setCookieMetaRegExp = /<meta http-equiv=[\"']?set-cookie[\"']? content=[\"'](.*)[\"'].*>/ig;
var match = setCookieMetaRegExp.exec(htmlstring);
Run Code Online (Sandbox Code Playgroud)
但match不包含我需要的值。救命?
注意:正则表达式已经正确(请参见此处)。我只需要将其与字符串匹配即可。注意:我正在使用NodeJS
你们离得太近了!现在需要做的只是一个简单的循环:
var htmlString = '<meta http-equiv="Set-Cookie" content="COOKIE1_VALUE_HERE">\n'+
'<meta http-equiv="Set-Cookie" content="COOKIE2_VALUE_HERE">\n'+
'<meta http-equiv="Set-Cookie" content="COOKIE3_VALUE_HERE">\n';
var setCookieMetaRegExp = /<meta http-equiv=[\"']?set-cookie[\"']? content=[\"'](.*)[\"'].*>/ig;
var matches = [];
while (setCookieMetaRegExp.exec(htmlString)) {
matches.push(RegExp.$1);
}
//contains all cookie values
console.log(matches);
Run Code Online (Sandbox Code Playgroud)
JSBIN:http://jsbin.com/OpepUjeW/1/edit?js, console
| 归档时间: |
|
| 查看次数: |
9279 次 |
| 最近记录: |