0

我是 Jquery 的新手。我有类似的字符串Dear < EmployeeList >, kindly complete your joining documentation and submit the same to < DepartmentList > latest by < DateCalendar >

我想将“< EmployeeList >”替换为“ABC”。

同样的规则也适用于其他 "< >" 。整个字符串不是固定的,它可以像那样改变。

Dear < EmployeeList >, welcome to < FreeTextCompany > We wish you a long and prosperous association with us.

所以请记住字符串不是固定的,它只是一个模板。

请帮我

我正在尝试找出字符串并替换为我的字符串但能够做到这一点

  var start_pos = test_str.indexOf('<') + 1;
            var end_pos = test_str.indexOf('<', start_pos);
            var text_to_get = test_str.substring(start_pos, end_pos)
            $('#MainContent_lblSMSTemplate').text().replace(text_to_get, "");
4

2 回答 2

1

干得好:

function replaceString(string, replaceObj) {
   for (var key in replaceObj) {
      string = string.replace(new RegExp(key, 'gi'), replaceObj[key]);
   }

   return string;
}

var replaceObject = {
     '< EmployeeList >' : 'ABC',
     '< DepartmentList >' : 'DepHr',
     '< DateCalendar >' : '12march'
};

var string = 'Dear < EmployeeList >, kindly complete your joining documentation and submit the same to < DepartmentList > latest by < DateCalendar >';

var newString = replaceString(string, replaceObject); // ouput will be: Dear ABC, kindly complete your joining documentation and submit the same to ABC latest by ABC
于 2013-09-05T18:25:04.943 回答
0

这是一个简单的解决方案:

var textstring = "Dear < EmployeeList >, kindly complete your joining documentation and submit the same to < DepartmentList > latest by < DateCalendar >";

textstring.replace("< EmployeeList >", "ABC");
textstring.replace("< DepartmentList >", "DepHr");
textstring.replace("< DateCalendar >", "12 March");
于 2013-09-05T18:21:58.407 回答