Javascript正则表达式直到可选逗号

时间:2018-01-23 10:49:10

标签: javascript regex

我正在尝试从顶行的longs字符串中提取Version

https://regex101.com/r/9jsOai/1

输出中有时会出现错误的逗号。我很想抓住第1组" 03.06.07b.E"如果逗号出现以排除逗号。

完整字符串:

名称/ OID:model.0;值(OctetString):Cisco IOS软件,IOS-XE软件,Catalyst L3交换机软件(CAT3K_CAA-UNIVERSALK9-M),版本 03.06.07b.E ,RELEASE SOFTWARE(fc1) 技术支持:http://www.cisco.com/techsupport 版权所有(c)1986-2017,由Cisco Systems,Inc。提供 编译Mon 30-Oc

3 个答案:

答案 0 :(得分:2)

这很有效。



var regex = new RegExp("Version ([^,]*)(,)? RELEASE");

var value1 = `Name/OID: model.0; Value (OctetString): Cisco IOS Software, IOS-XE Software, Catalyst L3 Switch Software (CAT3K_CAA-UNIVERSALK9-M), Version 03.06.07b.E RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Mon 30-Oc`;
var value2 = `Name/OID: model.0; Value (OctetString): Cisco IOS Software, IOS-XE Software, Catalyst L3 Switch Software (CAT3K_CAA-UNIVERSALK9-M), Version 03.06.07b.E, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Mon 30-Oc`;

var match = regex.exec(value1);
console.log(match[1]);
var match = regex.exec(value2);
console.log(match[1]);




答案 1 :(得分:0)

也许你可以尝试Version (.*)(,)? RELEASE

https://regex101.com/r/9jsOai/4

答案 2 :(得分:0)

此正则表达式应该有效:/Version ([^,]*),? RELEASE/g。在您的原始正则表达式中,您将以任意次数捕获任何字符作为版本号。你想要匹配的是任何字符除逗号以外的任何字符,直到分隔逗号,即([^,]*)。逗号后面的问号使逗号可选。试一试here