Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

Regex remove trailing info from URL

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';

Open in new window

Eddie Shipman
Eddie Shipman
1 Solution
HainKurtSr. System AnalystCommented:
something like this
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";

Open in new window

Eddie ShipmanAll-around developerAuthor Commented:
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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now