jquery - how to parse a string in javascript and capture all text before the last <br> -
i have text in javascript this:
this text<br>this second line of text
and want have function return
this text
so find last
<br>
in text , give me before it.
if text simple, .split()
on <br>
var str = "this text<br>this second line of text" var result = str.split('<br>')[0];
if more complex, may worthwhile use browser's built in html parser, can operate on them dom nodes.
in case, this:
var div, result, str = "this text<br>this second line of text"; (div = document.createelement('div')).innerhtml = str; var result = div.firstchild.data;
...or perhaps little simpler jquery:
var str = "this text<br>this second line of text" var result = $('<div>',{html:str}).contents().first().text();
Comments
Post a Comment