Help with regular expressions

I have a long list of URLs in a list format. They all start with “http://” and end with a period (“.”)

The problem is the URLs are in the middle of the tag, but I want them at the end of the tag like the example below.

Here is an example:

<li>text here. text here. [B][COLOR="#FF0000"]url.[/COLOR][/B] text here.</li>

I want to automatically replace the URLs so the url is at the end like this:

<li>text here. text here. text here. [B][COLOR="#FF0000"]url.[/COLOR][/B]</li>

Is this possible to do with regular expressions?

Are the URLs always the third item in the li tag?



Usually yes but not always. Even if I had something that works 75% of the time it would save me a ton of work.

I’ve been trying some test code, no success, yet… I’ll keep plugging at it, from time to time, see if I can get something for you.

Okay… I think I’ve got something, for you. I’m using jQuery, but just for the selector, for brevity.

<li>Text.  More text.  Another text.</li>
<script type="text/javascript">
          var thisHTML = $(this).html();
          thisHTML = thisHTML.replace(/^(.+\\. +)+(https?.+[^. ]\\. +)(.+\\. *)*$/gi,"$1$3 $2");

Hope this helps. (I also hope this isn’t for homework. I’d feel real lousy if I just gave you an answer.)

Thanks for that. No it isn’t for homework. Question: how do I run it?

That depends upon your current code and how you plan to activate it. The example I offered is set so that when the <li> is clicked, it will re-arrange things so that ANYWHERE within the li, if there is a URL, it will be put to the end. Unless it’s already at the end… or if it’s at the beginning, it may not work… I’m not sure… didn’t test for that.

You can set any event you like (click, load, mouseover, a change in another element, etc.) What is the context of your site/app?

I have it running now. But it seems to only work for the first list item and not any list item below it.

Is there a way to make it so that regardless of where the URL is in the tag, it automatically goes to the end of it?

Thanks again - I appreciate the help

The selector of the code I provided is jQuery shorthand for all <li> elements, I think. If you have more than one li and the click is only working on the first, I can’t understand why. Please provide your relevant code… maybe I can see something.

UPDATE: Ah. Gave it some more thought… I didn’t use .each(). Perhaps that will only affect the first one. Do you know enough about jQuery to modify that to work within .each()?

Taking a closer look at it, it’s not as robust as I thought it might be. Won’t work if the URL is first, and even if it’s in the second slot, it won’t work because the order of the backreferences is set, not dynamic.

Still… it worked better this way than when I tried splitting it into an array and going from there.

I’ll keep stabbing at it, when I can.

I’ve not had chance to test against all possible combinations, but how about this?

$('li').each(function(index) {
  var thisHTML = $(this).html();
  thisHTML = thisHTML.replace(/^(.*?)(http[^\\s]+)(.*)$/gi,"$1$3 $2");

Okay… I think I’ve got a better solution, now. :slight_smile:

    var thisHTML = $(this).html();
    thisHTML = thisHTML.replace(/\\.( +)/gi,'. |$1');
    var thisHTMLary = thisHTML.split('|'), thsMsg = '';
    for(var x in thisHTMLary){
        thsMsg += x + ": " + thisHTMLary[x] + "\
        if(thisHTMLary[x].indexOf('http') > -1){
            var thisURL = thisHTMLary.splice(x,1).toString();
            thisURL = " " + thisURL;
    thisHTML = thisHTMLary.join('');

And I just tested it with more than one <li> tag, all of them work, without the .each(), in FireFox, IE8, and Chrome.

… and one more thing I just thought of. TRIM THE WHITESPACE. :slight_smile:


… and I just realized that this will not work IF there is no period after the URL. Period must be there.

This can also be done like:


… AAAAAAAAAAAAND… I haven’t tested it, however, I believe that if you keep the break; in, it will only push one URL to the end - but if you remove it, I think it will put all URLs (if more than one) at the end.

UPDATE: NOPE! It won’t. But only because after the first slice/push, the indeces have changed by one. :shifty:

Just following up to see if any of these worked to satisfaction?

This works - thanks so much!! And sorry for being MIA… I had to put the work on the back burner due to other stuff that came up.

It will save me a ton of time today. Thanks again.

No worries about the time. Life happens. Glad it’s working. :slight_smile: