有没有办法从正则表达式中匹配和删除逻辑运算符

我不想在正则表达式中使用 Match 函数时包含逻辑运算符(and、or、not)。我在下面尝试过,但它没有按预期工作。有人可以帮帮我吗。


我用于解析的字符串类型:示例:


1. Input -->'(Value1==6) and (Value2==0)?1:0'

Output --> ["Value1", "Value2"]


2. Input : 'Value_1'

Output -->["Value_1"]


3. Input : '(Value_1 * Value_2)'

Output : ["Value1", "Value2"]


4. Input : 'Value_Machine_Outcome==4?1:0'

Output : Value_Machine_Outcome

嵌套条件:无


条件是否始终在括号中:否, 我将在下一步中使用Math.evaluate评估它们


请举例如下:


const paragraph = '(Value1==6) and (Value2==0)?1:0';

const regex = /([a-zA-Z]+[a-zA-Z0-9_]+)(?<!and|or|not)/g;

const found = paragraph.match(regex);


console.log(found);


// expected output: Array ["Value1", "Value2"]


缥缈止盈
浏览 98回答 2
2回答

SMILET

试试这个正则表达式:([a-zA-Z]+[a-zA-Z0-9_]+)(?<![and|or|not]). 我刚刚更新了您的代码中的正则表达式,请测试并让我知道您是否有任何疑问。const paragraph = '(Value1==6) and (Value2==0)?1:0';const regex = /([a-zA-Z]+[a-zA-Z0-9_]+)(?<![and|or|not])/g;const found = paragraph.match(regex);console.log(found);

素胚勾勒不出你

您更新的问题完全改变了输入的性质。如果输入如此不同,您将需要匹配任何不以 、 或 以外的数字开头的“单词”&nbsp;and(or但这not符合您最初的尝试,所以我想这是有道理的) :const&nbsp;regex&nbsp;=&nbsp;/(?!and|or|not)\b[A-Z]\w*/gi;实例:const tests = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; str: "(Value1==6) and or not (Value2==0)?1:0",&nbsp; &nbsp; &nbsp; &nbsp; expect: ["Value1", "Value2"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; str: "Value_1",&nbsp; &nbsp; &nbsp; &nbsp; expect: ["Value_1"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; str: "(Value_1 * Value_2)",&nbsp; &nbsp; &nbsp; &nbsp; expect: ["Value_1", "Value_2"]&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; str: "Value_Machine_Outcome==4?1:0",&nbsp; &nbsp; &nbsp; &nbsp; expect: ["Value_Machine_Outcome"] // Note I put this in an array&nbsp; &nbsp; }];const regex = /(?!and|or|not)\b[A-Z]\w*/gi;for (const {str, expect} of tests) {&nbsp; &nbsp; const result = str.match(regex);&nbsp; &nbsp; const good = result.length === expect.length && result.every((v, i) => v === expect[i]);&nbsp; &nbsp; console.log(JSON.stringify(result), good ? "Ok" : "<== ERROR");}其工作原理是不允许and、or、 和not,并要求在单词边界 (&nbsp;\b) 处进行匹配。请注意,在测试中,我将Value_Machine_Outcome==4?1:0字符串的预期结果更改为数组,而不仅仅是字符串,就像所有其他结果一样。问题完全改变输入之前的原始答案:如果你想使用String.prototype.match,你可以对 a 使用正向后视(自 ES2018 起)(并匹配 a 之前的所有内容=:const&nbsp;regex&nbsp;=&nbsp;/(?<=\()[^=]+/g;实例:const paragraph = '(Value1==6) and (Value2==0)?1:0';const regex = /(?<=\()[^=]+/g;const found = paragraph.match(regex);console.log(found);// expected output: Array ["Value1", "Value2"]如果您同意循环,则可以通过使用捕获组来避免后向查找(因为它们仅在 ES2018 中添加):const regex = /\(([^=]+)/g;const found = [];let match;while (!!(match = regex.exec(paragraph))) {&nbsp; &nbsp; found.push(match[1]);}实例:const paragraph = '(Value1==6) and (Value2==0)?1:0';const regex = /\(([^=]+)/g;const found = [];let match;while (!!(match = regex.exec(paragraph))) {&nbsp; &nbsp; found.push(match[1]);}console.log(found);// expected output: Array ["Value1", "Value2"]在评论中你问:我的表达式也可以包含下划线。就像它可以是 value_1、value_2 一样。那里能行得通吗?我说会是因为上面的两个都匹配除了=.后来你说:当我的结构包含“Value_1”时它会忽略同样,以上两者都可以与Value_1和配合使用Value_2:第一的:const paragraph = '(Value_1==6) and (Value_2==0)?1:0';const regex = /(?<=\()[^=]+/g;const found = paragraph.match(regex);console.log(found);// expected output: Array ["Value1", "Value2"]第二:const paragraph = '(Value_1==6) and (Value_2==0)?1:0';const regex = /\(([^=]+)/g;const found = [];let match;while (!!(match = regex.exec(paragraph))) {&nbsp; &nbsp; found.push(match[1]);}console.log(found);// expected output: Array ["Value1", "Value2"]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript