替换字符串中的占位符值

Replacing placeholder values within a string

本文关键字:占位符 字符串 替换      更新时间:2023-09-26

不确定为什么我的代码不工作,因为我从下面获取的代码在JSFiddle中工作

String.prototype.interpolate = (function() {
  var re = /'[(.+?)']/g;
  return function(o) {
    return this.replace(re, function(_, k) {
      return o[k];
    });
  }
}());
var _obj = {
  hey: 'Hey',
  what: 'world',
  when: 'today'
}
document.write(
  '[hey]-hey, I saved the [what] [when]!'.interpolate(_obj)
);

但我下面的代码似乎并没有做同样的事情。它仍然具有括号值[NAME][ADDRESS][PHONE],而不是替换的数据值:

$(document).ready(function () {
    $('#goSearching').click(function () {
        var isNumber    = $.isNumeric($('#searchBox').val());
        var theSearch   = $('#searchBox').val();
        var theURL      = '';
        if (isNumber) {
            if (theSearch.length >= 10) {
                //Search by NPI#
                $('#titleOfSearchResult').html('P search results by NPI #:');
                theURL = 'http://zzzzz.com:151/P/GetNPI/';
            } else {
                //Search by P #
                $('#titleOfSearchResult').html('P search results by P #:');
                theURL = 'http://zzzzzz.com:151/P/GetNo/';
            }
        } else {
            //Search by P Name
            $('#titleOfSearchResult').html('P search results by P Name:');
            theURL = 'http://zzzzz.com:151/P/PName/';
        }
        $.ajax({
            url         : theURL + $('#searchBox').val() + '/',
            type        : 'GET',
            dataType    : 'xml',
            timeout     : 10000,
            cache       : false,
            crossDomain : true,
            success     : function (xmlResults) {
                console.log(xmlResults);
                var _htmlObj = {};
                $(xmlResults).find("P").each(function() {
                    _htmlObj = {
                        NAME    : $(this).find("pName").text(),
                        ADDRESS : $(this).find("pSpecialty").text(),
                        PHONE   : $(this).find("pUserid").text()
                    }
                    console.log($(this).find("pName").text());
                    console.log($(this).find("pSpecialty").text());
                    console.log($(this).find("pUserid").text());
                    $("#theResults").append(                        
                        '<p><strong>[NAME]</strong></p>' +
                        '<p>[ADDRESS]</p>' +
                        '<p>[PHONE]</p>' +
                        '<p>&nbsp;</p>'.interpolate(_htmlObj)           
                    );
                });
                },
            error       : function(jqXHR, textStatus) {
                console.log("error: " + textStatus);
                }
        });
    });
    String.prototype.interpolate = (function () {
        var re = /'[(.+?)']/g;
        return function (o) {
            return this.replace(re, function (_, k) {
                return o[k];
            });
        }
    }());
});

在控制台中,它输出正确的返回值,但不会将这些值替换为占位符[NAME][ADDRESS][PHONE]

任何解决这个问题的帮助都将是伟大的!谢谢

您只在最后一个字符串上调用interpolate

'<p><strong>[NAME]</strong></p>' +
'<p>[ADDRESS]</p>' +
'<p>[PHONE]</p>' +
'<p>&nbsp;</p>'.interpolate(_htmlObj) 

成员访问的优先级高于添加,因此需要:

('<p><strong>[NAME]</strong></p>' +
'<p>[ADDRESS]</p>' +
'<p>[PHONE]</p>' +
'<p>&nbsp;</p>').interpolate(_htmlObj) 

来自@TrueBlueAussie:jsfiddle.net/TrueBlueAussie/fFSYA/9

的示例