简体   繁体   English

如何在Javascript中使用.replace?

[英]How to use .replace in Javascript?

I have some Javascript I am using on my site that automatically generates breadcrumbs based on my file structor. 我在网站上使用了一些Javascript,该Javascript可根据文件构造函数自动生成面包屑。 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. 我想做的是删除News_events中的低分,并用空格代替...与网站上的所有面包屑相同。

My Java Script is not very good but I think I have to add something like this: 我的Java脚本不是很好,但是我想我必须添加如下内容:

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

I'm just not sure where or how to place it with in the Javascript. 我只是不确定在Java脚本中的位置或放置方式。

Here is the full Javascript: 这是完整的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<xi;y++){ output += "../"; } output += bits[i] + "/\\">" + bits[i] + "</a> &nbsp;/&nbsp; "; } document.write(output + document.title); } 

Any help would be greatly appreciated! 任何帮助将不胜感激!

You just need to replace this line, so that bits[i] has the underscores replaced: 您只需要替换此行,以便bits[i]替换为下划线即可:

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

Turns into: 变成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM