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

How can i split the string containing <br/> tag using jquery. I tried the following code but it get error in console. I am not sure how to split the string based on <br/> tag Here is the code what i tried

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

Any suggestion would be great.

Answers:

Answer

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);
        });
    });
Answer

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.

Answer

Try using this:

var exploded = lines.split('<br />'); 

more info here

Answer

What if you try:

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

Can't you just do something like:

 var lines = 'this is for testing <br/> How are you<br/>'.split('<br/>');

Just as a side note if you want to get rid of the empty elements you can filter the lines like so:

// this will get rid of empty elements 
lines = lines.filter(function(n) { 
    return n; 
});
Answer
jQuery(document).ready(function($)
{

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

No need to wrap in jquery like :

jQuery('this is for testing <br/> How are you<br/>').split('<br/>');

Can be like :

('this is for testing <br/> How are you<br/>').split('<br/>');

DEMO

Answer

Try this

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

Demo

Answer

Here is the Working code

jQuery(document).ready(function($)
    {

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

That's no jQuery solution, but hopefully somebody will find it useful. The function returns one or two elements.

function splitContentsOnBr(el) {
    const before = document.createElement('div');
    const after = document.createElement('div');
    let found = false;
    el.childNodes.forEach(c => {
        if (found) {
            after.appendChild(c.cloneNode(true));
        } else if (c.tagName == 'BR') {
            found = true;
        } else {
            before.appendChild(c.cloneNode(true));
        }
    });
    return after.childNodes.length ? [before, after] : [before];
}
document.querySelector('#result').innerHTML = splitContentsOnBr(document.querySelector('h1'))
    .map(el => el.textContent.trim())
    .join(', ');
<h1>
  First part
  <br>
  Second part
</h1>
<div id="result">
</div>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.