[英]Javascript Regex to convert dot notation to bracket notation
Consider this javascript: 考虑这个javascript:
var values = {
name: "Joe Smith",
location: {
city: "Los Angeles",
state: "California"
}
}
var string = "{name} is currently in {location.city}, {location.state}";
var out = string.replace(/{([\w\.]+)}/g, function(wholematch,firstmatch) {
return typeof values[firstmatch] !== 'undefined' ?
values[firstmatch] : wholematch;
});
This will output the following: 这将输出以下内容:
Joe Smith is currently in {location.city}, {location.state}
But I want to output the following: 但我想输出以下内容:
Joe Smith is currently in Los Angeles, California
I'm looking for a good way to convert multiple levels of dot notation found between braces in the string into multiple parameters to be used with bracket notation, like this: 我正在寻找一种很好的方法将字符串中大括号之间的多个点符号转换为多个参数,以便与括号表示法一起使用,如下所示:
values[first][second][third][etc]
Essentially, for this example, I'm trying to figure out what regex string and function I would need to end up with the equivalent of: 从本质上讲,对于这个例子,我试图弄清楚我需要什么样的正则表达式字符串和函数,结果相当于:
out = values[name] + " is currently in " + values["location"]["city"] +
values["location"]["state"];
NOTE: I'd like to do this without using eval()
. 注意:我想在不使用eval()
情况下执行此操作。
Using a helper function to iteratively access the properties: 使用辅助函数迭代访问属性:
function getNestedValue(obj, prop) {
var value, props = prop.split('.'); // split property names
for (var i = 0; i < props.length; i++) {
if (typeof obj != "undefined") {
obj = obj[props[i]]; // go next level
}
}
return obj;
}
var string = "{name} is currently in {location.city}, {location.state}";
var out = string.replace(/{([^}]+)}/g, function(wholematch,firstmatch) {
var value = getNestedValue(joe, firstmatch);
return typeof value !== 'undefined' ? value : wholematch;
});
// "Joe Smith is currently in Los Angeles, California"
Try the above example here . 在这里尝试上面的例子。
Edit : Something slightly elegant , using the Array.prototype.reduce
method, part of the new ECMAScript 5th Edition Standard: 编辑 :略微优雅 ,使用Array.prototype.reduce
方法,新ECMAScript第5版标准的一部分:
function replace(str, obj) {
return str.replace(/{([^}]+)}/g, function(wholematch,firstmatch) {
var value = firstmatch.split('.').reduce(function (a, b) {
return a[b];
}, obj);
return typeof value !== 'undefined' ? value : wholematch;
});
}
replace("{name} is currently in {location.city}, {location.state}", values);
// "Joe Smith is currently in Los Angeles, California"
I'm not sure how to integrate this in Javascript, but here's a Java snippet that uses regex to do the transformation you need: 我不确定如何在Javascript中集成它,但这是一个使用正则表达式进行所需转换的Java代码片段:
String[] tests = {
"{name}",
"{location.city}",
"{location.state.zip.bleh}",
};
for (String test : tests) {
System.out.println(test.replaceAll("\\{?(\\w+).?", "[$1]"));
}
This prints: 这打印:
[name]
[location][city]
[location][state][zip][bleh]
Essentially it replaces all occurrences of \\{?(\\w+).?
基本上它取代了所有出现的\\{?(\\w+).?
to [$1]
. 到[$1]
。
You can take a look at the topic Error-free deep property access? 您可以查看主题无错误的深度属性访问? posted at comp.lang.javascript - this presents several approaches for doing what you want. 发布在comp.lang.javascript - 这提供了几种方法来做你想要的。
For the regexp, all you need is /{.*?}/g
对于正则表达式,您只需要/{.*?}/g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.