亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法從正則表達式中匹配和刪除邏輯運算符

有沒有辦法從正則表達式中匹配和刪除邏輯運算符

縹緲止盈 2023-09-21 14:17:06
我不想在正則表達式中使用 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"]
查看完整描述

2 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

試試這個正則表達式:([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);


查看完整回答
反對 回復 2023-09-21
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

您更新的問題完全改變了輸入的性質。如果輸入如此不同,您將需要匹配任何不以 、 或 以外的數字開頭的“單詞” andor但這not符合您最初的嘗試,所以我想這是有道理的) :

const regex = /(?!and|or|not)\b[A-Z]\w*/gi;

實例:

const tests = [

    {

        str: "(Value1==6) and or not (Value2==0)?1:0",

        expect: ["Value1", "Value2"]

    },

    {

        str: "Value_1",

        expect: ["Value_1"]

    },

    {

        str: "(Value_1 * Value_2)",

        expect: ["Value_1", "Value_2"]

    },

    {

        str: "Value_Machine_Outcome==4?1:0",

        expect: ["Value_Machine_Outcome"] // Note I put this in an array

    }

];


const regex = /(?!and|or|not)\b[A-Z]\w*/gi;

for (const {str, expect} of tests) {

    const result = str.match(regex);

    const good = result.length === expect.length && result.every((v, i) => v === expect[i]);

    console.log(JSON.stringify(result), good ? "Ok" : "<== ERROR");

}

其工作原理是不允許andor、 和not,并要求在單詞邊界 ( \b) 處進行匹配。請注意,在測試中,我將Value_Machine_Outcome==4?1:0字符串的預期結果更改為數組,而不僅僅是字符串,就像所有其他結果一樣。


問題完全改變輸入之前的原始答案:

如果你想使用String.prototype.match,你可以對 a 使用正向后視(自 ES2018 起)(并匹配 a 之前的所有內容=

const regex = /(?<=\()[^=]+/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))) {

    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))) {

    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))) {

    found.push(match[1]);

}


console.log(found);


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


查看完整回答
反對 回復 2023-09-21
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號