替换字符串中的占位符值

时间:2015-03-26 16:52:33

标签: javascript jquery prototype

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

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]

解决此问题的任何帮助都会很棒!谢谢!

1 个答案:

答案 0 :(得分:2)

您只在最后一个字符串

上调用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

相关问题