0

I have a following html string in which a date range has to be replaced.

 var htmlStr = "<strong>1-Mar-2015</strong>"+
     "<span> to </span>"+
     "<strong>15-Feb-2016</strong><br>"+
     "<span>by </span>"+
     "<strong>Week</strong><br>"+
     "<span>Key</span>"+
     "<span> is </span>"+
     "<strong>Key Value</strong><br>"+
     "<span>Type</span>"+
     "<span> is </span>"+
     "<strong>Report</strong>"+
     "<span> or </span>"+
     "<strong>Incident</strong><br>"+
     "<span>SomeKey</span>"+
     "<span> is </span>"+
     "<strong>SomeValue</strong>";

I want to replace the date range with some specific date range. For now i am breaking the html string with br tag to divide different properties and trying to replace the content of first and second strong tag.

I am looking for possible Regex solution to replace date range.

The js code flow is as follows:

var startdate = '2-Jun-2012';//selected from dom
var enddate = '24-Sept-2014';//selected from dom
for(var i = 0; i < IDList.length; i++ ) {
    var htmstr = loadHtmlDataFromServer(IDList[i]); //will get html type content from server
    //if startdate and enddate value exist, update htmstr
    CreateModal(htmlstr, IDList[i]);
}
3
  • 1
    you know for fact the the date will be display in strong tags? Commented Mar 20, 2016 at 10:28
  • yes it will be in strong tags and its a string. Commented Mar 20, 2016 at 10:30
  • you can try $("strong:eq(0)").text(startDate) and $("strong:eq(1)").text(endDate) Commented Mar 20, 2016 at 10:33

1 Answer 1

1

You tagged this jQuery:

var $html = $(hmtlstr),$dates=$html.find("strong");
$dates.eq(0).text(newFromDateString);
$dates.eq(1).text(newToDateString);
Sign up to request clarification or add additional context in comments.

2 Comments

Yes we can do that, +1 for the solution, but i was thinking of using string manipulation... won't it be better.
It us not recommended to do string manipulation on html - especially not if you have a Dom manipulating library loaded anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.