Priti Chaturvedi
Priti Chaturvedi

Reputation: 67

Find string between two character in whole string

I am new in Jquery.I have string like Dear < EmployeeList >, kindly complete your joining documentation and submit the same to < DepartmentList > latest by < DateCalendar >

I want to replace "< EmployeeList >" to "ABC".

And same rule is apply for other "< >" . Whole String is not fixed it could be change like that.

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

So please keep in mind string is not fixed it just a template.

Please help me

I am trying to findout string and replace to my string but able to do that

  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, "");

Upvotes: 0

Views: 223

Answers (2)

Jonathan Crowe
Jonathan Crowe

Reputation: 5803

here you go:

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

Upvotes: 1

Praxis Ashelin
Praxis Ashelin

Reputation: 5217

This is an easy solution:

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");

Upvotes: 0

Related Questions