1

I have some Javascript I am using on my site that automatically generates breadcrumbs based on my file structor. The problem I am having is that the names it displays are based on the file names, and some times those names have under scores in them. here is an example:

Home / Marketing / News_events / News & Events Home Page

What I would like to do is remove the under score in News_events and replace it with a space... same with all the bread crumbs on the site.

My Java Script is not very good but I think I have to add something like this:

string.replace(/_/g,' ');

I'm just not sure where or how to place it with in the Javascript.

Here is the full Javascript:

	
		function breadcrumbs(){
  sURL = new String;
  bits = new Object;
  var x = 0;
  var stop = 0;
  var output = '<a href="/"><i class="fa fa-home fa-lg"></i></a> &nbsp;/&nbsp; '; 
  sURL = location.href;
  sURL = sURL.slice(8,sURL.length); 
  chunkStart = sURL.indexOf("/");
  sURL = sURL.slice(chunkStart+1,sURL.length)
  while(!stop){
    chunkStart = sURL.indexOf("/");
    if (chunkStart != -1){
      bits[x] = sURL.slice(0,chunkStart)
      sURL = sURL.slice(chunkStart+1,sURL.length);
    }else{
      stop = 1;
    }
    x++;
  }
  for(var i in bits){
    output += "<a href=\"";
    for(y=1;y<x-i;y++){
      output += "../";
    }
    output += bits[i] + "/\">" + bits[i] + "</a> &nbsp;/&nbsp; ";
  }
  document.write(output + document.title);
}
		

Any help would be greatly appreciated!

5
  • 4
    Your question isn't clear to me but you need to use what replace returns: string = string.replace(/_/g,' '); Commented Jan 27, 2016 at 19:59
  • for a single char replacement, why use a regex? .replace('_', ' ') would be far more efficient. Commented Jan 27, 2016 at 20:00
  • 1
    @MarcB without the global tag, it will only replace the first instance. You need to use regex to replace all of the instances in the string. Commented Jan 27, 2016 at 20:01
  • @MarcB if the OP didnt state it would be used once, its better to assume there is an unknown amount of characters needing a replacement, to avoid the follow up question of "Why is my .replace only replacing a value once" Commented Jan 27, 2016 at 20:01
  • How and where do I put it in the Javascript? Commented Jan 27, 2016 at 20:05

1 Answer 1

1

You just need to replace this line, so that bits[i] has the underscores replaced:

output += bits[i] + "/\">" + bits[i] + "</a> &nbsp;/&nbsp; ";

Turns into:

output += bits[i] + "/\">" + bits[i].replace(/_/g,' ') + "</a> &nbsp;/&nbsp; ";

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

1 Comment

That did it!!!! Thank you very much! It wont let me accept your answer for a couple of min.

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.