目前我有两个表达式,我想要组合以及修复第二个表达式。表达式如下所示。脚本的输入如下:
Value/1.2 (Value; And Value Value Value 2_4_6) ValueAgain/3.5.7 (Value, Value Value) Value/4.6.8 Value/5.7.9
/\(.*?\)/
(获取括号外的所有文字)/\s*([\w]+?)\/([\d\w\W]+?)(\s+|\z)/g
(将Value/1.2.b
拆分为与此['Value', '1.2.b']
相等的数组)使用这些表达式,我目前运行以下脚本。
var a = 'Value/1.2 (Value; And Value Value Value 2_4_6) ValueAgain/3.5.7 (Value, Value Value) Value/4.6.8 Value/5.7.9', b = /\s*([\w]+?)\/([\d\w\W]+?)(\s+|\z)/g, c = a.split(/\(.*?\)/);
console.log(a, c);
for(var i = 0; i < c.length; i++) {
console.log('Exec: ', b.exec(c[i]), ' Split: ', c[i].split(b));
}
此脚本日志如下所示。
Value/1.2 (Value; And Value Value Value 2_4_6) ValueAgain/3.5.7 (Value, Value Value) Value/4.6.8 Value/5.7.9 ["Value/1.2 ", " ValueAgain/3.5.7 ", " Value/4.6.8 Value/5.7.9"]
Exec: ["Value/1.2 ", "Value", "1.2", " "] Split: ["", "Value", "1.2", " ", ""]
Exec: ["n/3.5.7 ", "n", "3.5.7", " "] Split: ["", "ValueAgain", "3.5.7", " ", ""]
Exec: null Split: ["", "Value", "4.6.8", " ", "Value/5.7.9"]
Value/5.7.9
&#34;。答案 0 :(得分:0)
您可以将两个正则表达式转换为字符串并构建一个新表达式。例如:
var rega = /(.*?)/,
regb = /(^[>]+)/,
regc = new RegExp
(
rega.toString().toString().slice(1, -1) +
regb.toString().toString().slice(1, -1),
'g'
);
会导致regc
成为/(.*?)(^[>]+)/g
。