解析&在JavaScript中检查条件表达式

发布于 2025-01-23 02:05:44 字数 560 浏览 2 评论 0原文

我的字符串中有可变条件,因此:

( @is_verified ='true'或@confirmation_needed!='true')和@request_limit!='0'

这只是一个示例,并且有未知数的变量&案例。

  1. 每个变量都以 @ @
  2. 有时有括号值的ors和s和shiss的
  3. 引号,例如'xyz',因此所有变量都可以视为字符串。
  4. 检查条件

JavaScript映射

const vars = {
 IS_VERIFIED: 'True',
 CONFIRMATION_NEEDED: 'True',
 REQUEST_LIMIT: '2'
}

始终使用=或!=我也有一个 。我提出了很少的想法,但它们都太复杂了。是否有任何已知的简单方法,或者我必须从头开始创建一些东西?

感谢任何帮助,谢谢。

编辑:实现这一目标后,对我来说,下一个目标将显示哪些变量以某种方式破坏了状况。

I have string that have variable conditions in it, like this:

(@IS_VERIFIED = 'True' OR @CONFIRMATION_NEEDED != 'True') AND @REQUEST_LIMIT != '0'

This is just an example and there are unknown number of variables & cases.

  1. Every variable starts with @
  2. There are ORs and ANDs with sometimes parenthesis
  3. Values are always in quotation marks like 'xyz', so all can be considered strings.
  4. Conditions are always checked with either = or !=

I also have a javascript map which holds all the variables like:

const vars = {
 IS_VERIFIED: 'True',
 CONFIRMATION_NEEDED: 'True',
 REQUEST_LIMIT: '2'
}

What I need to do is parse the condition string and check the values using the map above to reach a final result, true or false. I came up with few ideas but they were all too complicated. Is there any known, easy method for this or do I have to create something from scratch?

Any help is appreciated, thanks.

Edit: After achieving this, next goal for me will be showing which variables break the condition, somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2025-01-30 02:05:44

注意:eval提前解决方案,在使用此问题时要非常小心!

只需修改字符串为有效的JS表达式,然后使用eval

const vars = {
  IS_VERIFIED: "True",
  CONFIRMATION_NEEDED: "True",
  REQUEST_LIMIT: "2",
};

const str = `(@IS_VERIFIED = 'True' OR @CONFIRMATION_NEEDED != 'True') AND @REQUEST_LIMIT != '0'`;

const evalStr = str
  .replaceAll("@", "vars.")
  .replaceAll("OR", "||")
  .replaceAll("AND", "&&")
  .replaceAll("!=", "!")
  .replaceAll("=", "===")
  .replaceAll("!", "!==");

const res = eval(evalStr);
console.log(res);

Caution: eval solution ahead, be very careful while using this!

Simply modify the string to be a valid JS expression and then use eval.

const vars = {
  IS_VERIFIED: "True",
  CONFIRMATION_NEEDED: "True",
  REQUEST_LIMIT: "2",
};

const str = `(@IS_VERIFIED = 'True' OR @CONFIRMATION_NEEDED != 'True') AND @REQUEST_LIMIT != '0'`;

const evalStr = str
  .replaceAll("@", "vars.")
  .replaceAll("OR", "||")
  .replaceAll("AND", "&&")
  .replaceAll("!=", "!")
  .replaceAll("=", "===")
  .replaceAll("!", "!==");

const res = eval(evalStr);
console.log(res);

过期以后 2025-01-30 02:05:44

这是使用Regexp的片段:

let test = "@XYZ='True' AND @ABC='False'";

const regexp = /(@\w+)|(\'\w+\')/g;

const regexpResult = [...test.match(regexp)]
// Returns something like this:
// ["@XYZ", "'True'", "@ABC", "'False'"]

const vars = {};

for (let i = 0; i < regexpResult.length; i += 2) // We will be using two consecutive values at a time
{
  let keyName = regexpResult[i].replace("@", "");
  let keyValue = regexpResult[i+1].replaceAll("'", "");
  
  vars[keyName] = keyValue;
}

// OUTPUT of "vars":
// {
//   XYZ: "True",
//   ABC: "False"
// }

我希望这使您了解如何解决问题。

Here is a snippet using RegExp:

let test = "@XYZ='True' AND @ABC='False'";

const regexp = /(@\w+)|(\'\w+\')/g;

const regexpResult = [...test.match(regexp)]
// Returns something like this:
// ["@XYZ", "'True'", "@ABC", "'False'"]

const vars = {};

for (let i = 0; i < regexpResult.length; i += 2) // We will be using two consecutive values at a time
{
  let keyName = regexpResult[i].replace("@", "");
  let keyValue = regexpResult[i+1].replaceAll("'", "");
  
  vars[keyName] = keyValue;
}

// OUTPUT of "vars":
// {
//   XYZ: "True",
//   ABC: "False"
// }

I hope this gives you an idea about how to solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文