Javascript - 替换文本中基于对象的值的最快、最有效的方法

发布于 2024-12-10 16:08:33 字数 406 浏览 1 评论 0原文

我有一个看起来像这样的对象:

var obj = {
    a: "text",
    b: "text 2",
    c: "text 3",
    ...
}

我有一堆看起来像这样的字符串:

var stringA = "http://{{a}}.something.com/",
    stringB = "http://something.{{b}}.com/",
    stringC = "http://something.com/{{c}}";

我想通过遍历 obj 将 {{(\w)}} 替换为等效的 并检查它是否具有每个字符串的匹配值,但我确信有更好更快的方法来做到这一点。

有什么想法吗?

I have an object that looks like this:

var obj = {
    a: "text",
    b: "text 2",
    c: "text 3",
    ...
}

and I have a bunch of strings that look like this:

var stringA = "http://{{a}}.something.com/",
    stringB = "http://something.{{b}}.com/",
    stringC = "http://something.com/{{c}}";

I want to replace {{(\w)}} with it's equivalent by going through obj and checking if it has my matched value for every string, but I'm sure there is a better and faster way to do so.

Any ideas?

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

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

发布评论

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

评论(3

孤凫 2024-12-17 16:08:33

Douglas Crockford 编写了一个名为 supplant 的函数,它几乎完全可以满足您的需求。我稍微改变了函数以匹配您的双花括号 -

if (typeof String.prototype.supplant !== 'function') {
    String.prototype.supplant = function (o) {
        return this.replace(/{{([^{}]*)}}/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' ? r : a;
        });
    };
}

var obj = {
    a: "text",
    b: "text 2",
    c: "text 3"
}

var stringA = "http://{{a}}.something.com/",
    stringB = "http://something.{{b}}.com/",
    stringC = "http://something.com/{{c}}";

alert(stringA.supplant(obj));

演示 - http://jsfiddle.net/saZGg/

Douglas Crockford wrote a function called supplant that does almost exactly what you want. I've altered the function slightly to match your double curly braces -

if (typeof String.prototype.supplant !== 'function') {
    String.prototype.supplant = function (o) {
        return this.replace(/{{([^{}]*)}}/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' ? r : a;
        });
    };
}

var obj = {
    a: "text",
    b: "text 2",
    c: "text 3"
}

var stringA = "http://{{a}}.something.com/",
    stringB = "http://something.{{b}}.com/",
    stringC = "http://something.com/{{c}}";

alert(stringA.supplant(obj));

Demo - http://jsfiddle.net/saZGg/

兔姬 2024-12-17 16:08:33
var arr = /(.*){{(\w)}}(.*)/.exec(stringA);
arr[2] = obj[arr[2]];
arr.shift(); // remove full string at start
var newString = arr.join("");
  • 一般对字符串运行正则表达式,
  • 执行并将其转换为数组。
  • 将对象散列中的值的组之一交换,
  • 将数组连接到“编译”字符串中。

或者使用 .replace 解决方案之一,这无疑更优雅。

var arr = /(.*){{(\w)}}(.*)/.exec(stringA);
arr[2] = obj[arr[2]];
arr.shift(); // remove full string at start
var newString = arr.join("");
  • Generally run a regular expression on the string,
  • execute and turn it into an array.
  • Swap one of the groups for the value in your object hash
  • join the array into your "compiled" string.

Or use one of the .replace solutions which is arguebly more elegant.

你曾走过我的故事 2024-12-17 16:08:33

应该这样做:

function format(str, values) {
    return str.replace(/{{(\w)}}/g, function(match, name) {
        return values[name] || match;
    });
}

它只是查找对象是否具有具有捕获名称的属性。如果没有,则不会替换任何内容。

用法:

var str = format(stringA, obj);

This should do it:

function format(str, values) {
    return str.replace(/{{(\w)}}/g, function(match, name) {
        return values[name] || match;
    });
}

It just looks up whether the object has a property with the captured name. If not, nothing is replaced.

Usage:

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