【问题标题】:RegEx Matching where input goes onto a new line输入进入新行的正则表达式匹配
【发布时间】:2022-06-10 22:36:49
【问题描述】:

我有以下 RegEx 模式:

const test = new RegExp('var _0x((?=\\d+[A-Za-z]+[\\w@]+|[a-zA-Z]+[\\w@]+)[\\w@]{6}) = {(.*?)}/', 'gm');
const res = test.exec(script);

我还有以下输入字符串:

var _0x48ff10 = {
    "Gxh": function _0x14cba1(_0x248470) {
        return _0x248470();
    }, "Zvp": function _0x79a852(_0x5e72a5, _0xb98ae2) {
        return _0x5e72a5 < _0xb98ae2;
    }, "Zol": function _0x536e02(_0x1c65c3, _0x4116c9) {
        return _0x1c65c3 + _0x4116c9;
    }
};

预期的匹配文本应该是:

{
    "Gxh": function _0x14cba1(_0x248470) {
        return _0x248470();
    },
    "Zvp": function _0x79a852(_0x5e72a5, _0xb98ae2) {
        return _0x5e72a5 < _0xb98ae2;
    },
    "Zol": function _0x536e02(_0x1c65c3, _0x4116c9) {
        return _0x1c65c3 + _0x4116c9;
    }
};

我的 RegEx 似乎因为输入字符串中的换行符而中断。

我该如何解决这个问题?

【问题讨论】:

  • 你想要regex101.com/r/rlK4WP/1这样的东西吗?
  • 好吧,你的正则表达式与变量声明不匹配,而不是花括号中的内容。

标签: node.js regex


【解决方案1】:

你需要使用dotAll选项,并且贪婪搜索,即

/... ({.*};)/gms

const script = `
var _0x48ff10 = {
    "Gxh": function _0x14cba1(_0x248470) {
        return _0x248470();
    }, "Zvp": function _0x79a852(_0x5e72a5, _0xb98ae2) {
        return _0x5e72a5 < _0xb98ae2;
    }, "Zol": function _0x536e02(_0x1c65c3, _0x4116c9) {
        return _0x1c65c3 + _0x4116c9;
    }
};
`;

const test = /var _0x((?=\d+[A-Za-z]+[\w@]+|[a-zA-Z]+[\w@]+)[\w@]{6}) = ({.*};)/gms;
const res = test.exec(script);
console.log(res[2]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多