Search code examples
jqueryparsingtextinsertafter

Insert line breaks using jQuery


I am grabbing some text from html file and assigning it to a variable. I would like to parse this variable for text: "AB" and insert line breaks after every instance.

$(flyout).click(function () {
     WinJS.xhr({ url: "http://somesite.html", responseType: "text" })
                        .done(function complete(result) {

     var rawText = result.responseText;
     var endResult = $(rawText).text();

     //need to parse endResult and add 2 line breaks and display in div fetchedtext


     fetchedtext.innerText = ParsedendResult;

Solution

  • var endResult = $(rawText).text();
    endResult = endResult.replace(/AB/g, "AB<br>");
    

    More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace