Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex remove trailing info from URL

Posted on 2011-09-02
2
Medium Priority
?
362 Views
Last Modified: 2012-06-27
I'm building a url dynamically using the code below.

What I'm in search of is a regex that will remove any TRAILING /0 in the URL

For instance, if my URL comes out like this, due to the programs and zip not being entered, 'schedule_eddie/TX/2011-10/0/0'

I want the URL to only be 'schedule_eddie/TX/2011-10'


No if my URL comes out like this, due to the programs only not being entered, 'schedule_eddie/TX/2011-10/0/0/75210|25'
I want it left alone as is. I only need to truncate if the trailing values are empty (value 0).
*Note: I CANNOT remove all '/0's because they are needed like in this last case.

  function buildURLandRedirect() {
    if(trim($('#state').val())<> '') {
      state = trim($('#state').val());
    } else {
      state = '0';
    }
    if(trim($('#months').val())<> '') {
      months = trim($('#months').val());
    } else {
      months = '0';
    }
    if(trim($('#programs').val())<> '') {
      programs = trim($('#programs').val());
    } else {
      programs = '0';
    }
    if(trim($('#zip').val())<> '') {
      zip = trim($('#zip').val());
    } else {
      zip = '0';
    }
    window.location='/schedule_eddie/'+state+'/'+months+'/'+programs+'/'+zip;
  }

Open in new window

0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 59

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 36475036
something like this
<script>
String.prototype.endsWith = function(suffix) { 
    return this.indexOf(suffix, this.length - suffix.length) !== -1; 
}; 

function fixURL(url){
  var fixedUrl = url;
  while (fixedUrl.endsWith("/0")) fixedUrl = fixedUrl.substr(0, fixedUrl.length-2);
  return fixedUrl;
}
var myUrl = "schedule_eddie/TX/2011-10/0/0/75210|25/0/0";
alert(fixURL(myUrl));
</script>

Open in new window

0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 36475356
I just ran into another problem that I will have to tackle and it means that I won't really need this after all, but thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question