0

I'm using Feedroll for showing RSS entries on my Tumblr.

Let's say I have:

document.write('<li class="rss-item"><a class="rss-item" href="http://mywebsite.com/arandomlink" target="_self"> [element1] element2 element3 element4</a><br />');
document.write('</li>');

element1 is a text "Thisismytext" (fixed length : 7)

element2 is a text "This Is My Text" (length not fixed)

element3 is a number "01" to "24" (fixed length : 2)

element4 is a text "THIS IS MY TEXT" (fixed length : 6 + space + 2)

(I have many entries and element1 and element4 don't change.)

I can't change the code from Feedroll or the titles of the entries.

Now, I would like to show my enrties ([element1] element2 element3 element4) like element 2 - element 3.

Example: [Thisismytext] This Is My Text 05 OTHER TEXT to This Is My Text - 05.

How do I do that?

I have tried split(), pop(), and substring() but I don't know how to use them.

And by the way, sorry for my English, I am French.

2
  • Try using regular expressions to match and parse the text values? Commented Jul 14, 2014 at 8:51
  • Well, I don't know exactly how to do that. But thank you for your answer. Commented Jul 14, 2014 at 8:53

1 Answer 1

1

Assuming you don't receive the elements individually and are just getting a bunch of markup that you want to reformat...

If you are receiving the string of text and you want to strip out the different elements, you might want to use a regular expression to match the elements.

The following expression would just matches the text [element1] element2 element3 element4 with the constraints that you mentioned.

http://www.regexr.com/3959m

regular expression : /\[(.{7})\]\s(.*)\s([0-9]{0,2})\s(.*)/g

sample text : [Thisism] This Is My Text 02 OTHER TEXT

matches :

enter image description here

If you are receiving the full HTML string, you'd have to alter the regular expression to handle the html tags.

More info on regular expressions here : http://www.regular-expressions.info/tutorial.html

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

5 Comments

I tried but I don't understand how to replace some elements of a string using regular expressions.
Okay so, I found how to do it : var re = /\[(.{7})\]\s(.*)\s([0-9]{0,2})\s(.*)/; var str = "[Thisism] This Is My Text 02 OTHER TEXT"; var newstr = str.replace(re, "$2 - $3"); But now, I would like to fetch every <li class="rss-item"><a class="rss-item" href="http://mywebsite.com/arandomlink" target="_self"> [element1] element2 element3 element4</a><br /></li> and automatically replace it with newstr. Is it possible?
sure. with jQuery $(".rss-item").each(function(rssItem) { // do your stuff here with rssItem.text()}) or .html(), you'll have to check
Btw, if it's more easy with PHP (I mean, the "search and replace" part), I have a private server.

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.