Expression表达式库

Janing

Expression 简单易用的表达式库

github:https://github.com/JuneAndGreen/expr-parser/tree/master

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

//直接运行
{
//获取Expression管理器
let expressionMgr =new ExpressionMgr()
//运行获取结果,这个方法会根据script缓存Expression对象,不会重复创建
let script = "1+1"
let result = expressionMgr.RunScript(script)
console.log(result); // 2

//清理缓存
expressionMgr.DeleteScript(script)

//清理全部缓存
expressiomMgr.Clear()
}

//全局管理器
{
let result = ExpressionMgr.instance().RunScript("")
}

//战斗中使用
{
let result = this.TF_Battle.ExpressionMgr.RunScript("")
}

简单表达式计算

1
2
3
4
5
expressionMgr.RunScript("\"\\u2605haha\"")          // "★haha"
expressionMgr.RunScript("+\"123\"") // "123"
expressionMgr.RunScript("-123") // -123
expressionMgr.RunScript("a.b.c.d",{a:{b:null}}) // null
expressionMgr.RunScript("]123") // undefined

逻辑运算

1
2
3
4
expressionMgr.RunScript("a.value + 12 - (2 * 14 / 4)", {a:{value:1}})       //6
expressionMgr.RunScript("a && b || c && ( d || e )",
{a:true, b:false, c:true, d:false, e:true}) //true
expressionMgr.RunScript("a[i+1]", {a: [0,5,10], i: 1}) //10

方法调用

1
2
3
4
5
6
7
8
9
10
11
expressionMgr.RunScript("a[\"b\"].c + a.d[\"e\"]", {a:{b:{c:1}}, d:{e:2}})  //3
expressionMgr.RunScript("a.b()",{
a: {
b: function(){
return this.c + this.d
},
c: 2,
d: 3,
}
}) //5

  • Title: Expression表达式库
  • Author: Janing
  • Created at : 2025-09-25 20:10:00
  • Updated at : 2025-09-25 20:34:14
  • Link: https://your-domain.com/Expression表达式库/
  • License: This work is licensed under CC BY-NC-SA 4.0.