我需要正则表达式匹配特定的关键字和最近的“,”字符。这是我的示例字符串:
ersion: V3 Subject: EMAILADDRESS=aa@example.com, GIVENNAME=Blob, SURNAME=Bloby, CN=B dddddddddddd, O=sd, C=IR Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5 Key: Sun RSA public key, 2048 bits modulus
现在我需要匹配EMAILADDRESS和下一个之间的单词,这将是“aa@example.com”(对于这两个点之间的单词没有限制,它们可以是任何字符。什么是合适的正则表达式对此?
答案 0 :(得分:2)
使用捕获组:
var str = 'ersion: V3 Subject: EMAILADDRESS=aa@example.com, GIVENNAME=Blob, SURNAME=Bloby, CN=B dddddddddddd, O=sd, C=IR Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5 Key: Sun RSA public key, 2048 bits modulus'
str.match(/EMAILADDRESS=([^,]+)/i)[1]
// => "aa@example.com"
[^,]
匹配任何字符异常,
。答案 1 :(得分:1)
只是为了完整,
var data = "version: V3 Subject: EMAILADDRESS=aa@example.com...", result = [];
data.split(",").forEach(function(currentItem) {
var idx = currentItem.indexOf("EMAILADDRESS=");
if (idx !== -1) {
result.push(currentItem.split("EMAILADDRESS=", 1)[1]);
}
});
console.log(result);
<强>输出强>
[ 'aa@example.com' ]