Split the string based on <br/> tag using jquery

Lots of duplicate answers here. This one is different. If you can guarantee the spelling of the <br/> tag, the other answers are fine. But if you have no control over the HTML, the line break tag could come in different formats:

<br/>
<BR/>
<br />
<br>
<br >
...etc.

major browsers can all handle all of these, but only the first will be handled by the suggested .split("<br/>") operation. A more robust option is to use a regular expression to match the tag:

jQuery(document).ready(function($)
{
    var brExp = /<br\s*\/?>/i;
    var lines = ("this is for testing <br/> How are you<BR />").split(brExp);
});

I've written the expression to be case-insensitive, allow any number of spaces after '<br', and the '/' is optional.


You want to split a vanilla string, don't pass it to $() rather simply;

jQuery(document).ready(function($)
    {
        var lines = 'this is for testing <br/> How are you<br/>'.split('<br/>');
        jQuery.each(lines, function() {
            alert(this);
        });
    });