0
html_string "<span class=\"verse\"><strong>1<\/strong>\u00a0hello world how are you?,<span class=\"trans\" title=\"\u00a0Greek brothers.\">t<\/span> I am fine thank you.<\/span><span class=\"verse\"><strong>2<\/strong>\u00a0this world is very bad.<\/span><span class=\"verse\"><strong>3<\/strong>\u00aall me are good,<\/span>"

I just want to extract the text inside the span which has class verse and it should not include text from span with class trans.

The result must be in array form.

from above string I must get result like this

["\u00a0hello world how are you? I am fine thank you","\u00a0this world is very bad.","\u00aall me are good,"]

Thanks

1 Answer 1

1

How about this:

var html_string = "<span class=\"verse\">"
                    + "<strong>1</strong>\u00a0hello world how are you?,"
                    + "<span class=\"trans\" title=\"\u00a0Greek brothers.\">t<\/span> "
                    + "I am fine thank you."
                + "</span>"
                + "<span class=\"verse\">"
                    + "<strong>2</strong>\u00a0this world is very bad."
                + "</span>"
                + "<span class=\"verse\">"
                    + "<strong>3<\/strong>\u00aall me are good,"
                + "</span>";

// Build up the DOM in a hidden element we can parse through
var $html = $('<div>',{html:html_string}).hide().appendTo('body');

// loop through each verse    
$html.find('.verse').each(function(i,e){
    // grab the verse, but first delete any span.trans
    var verse = $(e).find('span.trans').remove().end().text();
    // console.log(verse);
});
// remove the html from the DOM
$html.remove();

You should be able to loop it without adding it to the DOM, but for some reason I can't get it to work. Might be because it's late or my fingers are on strike for the evening. Either way, if someone finds a way to do it without attaching it, please post and I'll up-vote.

Sign up to request clarification or add additional context in comments.

Comments

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.