Tom Finger
Tom Finger

Reputation: 11

remove everything before or after a special character

I´m trying to change a list given by a RSS feed. The feed displays it like:

<ul>
    <li>
        <a title="Posted " href="" target="_blank">
            Chris Liebing in Castellaneta Marina - 21 June
       </a>
        <p>
           Chris Liebing, Clorophilla, Chlorophilla, Castellaneta Marina, Italy, 2014-06-21 
        </p>
    </li>
</ul>

The Result should look like:

<ul>
    <li>
        <a title="Posted " href="" target="_blank">
            21 June
       </a>
        <p>
            Clorophilla, Chlorophilla, Castellaneta Marina, Italy 
        </p>
    </li>
</ul>

I have to get the Date to the left and hide the Artist´s name (Chris Liebing) of the paragraph. Also to get rid of the last date 2014-06-21. There will be more lists displayed for different Artists, that´s why I thougt it´s the best to get rid of everything before the "-" in the link and hide everything before the first and after the last "," in the paragraph.

Upvotes: 1

Views: 1149

Answers (1)

adeneo
adeneo

Reputation: 318182

This would get rid of everything before the "-" in the link and hide everything before the first and after the last "," in the paragraph.

$('li a').text(function(_, txt) {
    return $.trim( txt.split('-').pop() ); // split on hyphen, get last part
});

$('li p').text(function(_, txt) {
    var parts = txt.split(','); // split on comma
    parts.shift();              // remove first
    parts.pop();                // remove last
    return parts.join(',');     // put back together again
});

FIDDLE

Upvotes: 1

Related Questions