我试图执行这样的代码:
value = value.replace(/{{(.*)}}/g, this.dataService.relationTable["\"$1\""]);
我希望将变量捕获到围绕它的{{和}}字符串中。然后,我想使用该值来搜索名为this.dataService.relationTable的对象,并找到等于正则表达式捕获值的键字符串。
有谁知道我做错了什么?
示例relationTable:
{C5: 200000, C7: 0, C6: 0, C8: "Site-Built", C26: 25, …}
C5
:
200000
C6
:
0
C7
:
0
C8
:
"Site-Built"
C11
:
4.25
C12
:
360
C13
:
3
C14
:
1000
C15
:
0
C16
:
0
C17
:
5000
C20
:
80
C21
:
8
C22
:
1000
C23
:
8
C24
:
0
C25
:
0
C26
:
25
C29
:
2000
C30
:
275
C31
:
250
C32
:
875
F4
:
"{{C5}}"
F5
:
"{{C7}}"
F6
:
"{{I21}}"
F7
:
"{{F24}}"
F8
:
"{{C29}}"
F9
:
"{{F4}} + {{F5}} + {{F6}} + {{F7}} + {{F8}}"
F11
:
"{{F9}}"
F12
:
"{{C6}}"
F13
:
"{{F11}} + {{F12}}"
F15
:
"{{C5}}"
F16
:
"{{C7}}"
F17
:
"{{C6}}"
F18
:
"{{F15}} + {{F16}} + {{F17}}"
F20
:
"{{F27}}"
F21
:
"{{C30}}"
F22
:
"{{C31}}"
F23
:
"{{C32}}"
F24
:
"{{F20}} + {{F21}} + {{F22}} + {{F23}}"
F26
:
"{{I11}}"
F27
:
"{{F26}} * 9 / 400"
I4
:
"{{F18}}"
I5
:
"{{F24}}"
I6
:
"{{C29}}"
I7
:
"{{C17}}"
I8
:
"{{I35}}"
I9
:
"{{I26}}"
I10
:
"{{C14}} * -1"
I11
:
"{{I4}} + {{I5}} + {{I6}} + {{I7}} + {{I8}} + {{I9}} - {{I10}}"
I13
:
"{{I11}}"
I14
:
"{{C11}}"
I15
:
"{{C12}}"
I16
:
undefined
I18
:
"{{I35}}"
I19
:
"{{C15}}"
I20
:
"{{C17}}"
I21
:
"{{I18}} + {{I19}} + {{I20}}"
I23
:
"{{F13}}"
I24
:
"{{C14}} * -1"
I25
:
"{{C13}}"
I26
:
"({{I23}} + {{I24}}) * {{I25}}"
I28
:
"{{C20}} * {{C21}}"
I29
:
"{{C22}} * {{C23}}"
I30
:
"{{C24}} * {{C25}}"
I31
:
"{{C16}} * -1"
I32
:
"{{C11}} * {{C26}} / 365"
I33
:
"{{C20}} * 12"
I34
:
"{{C24}} * 12"
I35
:
"{{I28}} + {{I29}} + {{I30}} + {{I31}} + {{I32}} + {{I33}} + {{I34}}"
样本值:
{{I35}}
在这种情况下,结果应为{{I28}} + {{I29}} + {{I30}} + {{I31}} + {{I32}} + {{I33}} + {{I34 }}
答案 0 :(得分:2)
使用函数替换:
value = value.replace(/{{(.*)}}/g, (match, p1) => this.dataService.relationTable[p1]);
此处,值p1
将采用正则表达式中第一个捕获组(...)
内匹配的值。
答案 1 :(得分:0)
我认为这是一个X / Y问题。您可能只想要:var key =“I35”,value = relationTable [key]
var relationTable = { C5: 200000, C6: 0, C7: 0, C8: "Site-Built", C11: 4.25, C12: 360, C13: 3, C14: 1000, C15: 0, C16: 0, C17: 5000, C20: 80, C21: 8, C22: 1000, C23: 8, C24: 0, C25: 0, C26: 25, C29: 2000, C30: 275, C31: 250, C32: 875, F4: "{{C5}}", F5: "{{C7}}", F6: "{{I21}}", F7: "{{F24}}", F8: "{{C29}}", F9: "{{F4}} + {{F5}} + {{F6}} + {{F7}} + {{F8}}", F11: "{{F9}}", F12: "{{C6}}", F13: "{{F11}} + {{F12}}", F15: "{{C5}}", F16: "{{C7}}", F17: "{{C6}}", F18: "{{F15}} + {{F16}} + {{F17}}", F20: "{{F27}}", F21: "{{C30}}", F22: "{{C31}}", F23: "{{C32}}", F24: "{{F20}} + {{F21}} + {{F22}} + {{F23}}", F26: "{{I11}}", F27: "{{F26}} * 9 / 400", I4: "{{F18}}", I5: "{{F24}}", I6: "{{C29}}", I7: "{{C17}}", I8: "{{I35}}", I9: "{{I26}}", I10: "{{C14}} * -1", I11: "{{I4}} + {{I5}} + {{I6}} + {{I7}} + {{I8}} + {{I9}} - {{I10}}", I13: "{{I11}}", I14: "{{C11}}", I15: "{{C12}}", I16: null, I18: "{{I35}}", I19: "{{C15}}", I20: "{{C17}}", I21: "{{I18}} + {{I19}} + {{I20}}", I23: "{{F13}}", I24: "{{C14}} * -1", I25: "{{C13}}", I26: "({{I23}} + {{I24}}) * {{I25}}", I28: "{{C20}} * {{C21}}", I29: "{{C22}} * {{C23}}", I30: "{{C24}} * {{C25}}", I31: "{{C16}} * -1", I32: "{{C11}} * {{C26}} / 365", I33: "{{C20}} * 12", I34: "{{C24}} * 12", I35: "{{I28}} + {{I29}} + {{I30}} + {{I31}} + {{I32}} + {{I33}} + {{I34}}"}
var key="I35", value=relationTable[key];
console.log(value);
// or
key="{{I35}}", value=relationTable[key.replace(/[\{\}]/g,"")];
console.log(value);