简体   繁体   English

Javascript:替换字符串中的特定单词

[英]Javascript: Replace specific words in string

I like to change URL in function of the language so for that I tried this code: 我喜欢更改语言功能中的URL,因此我尝试了以下代码:

var dec = {
  "agence": "agency",
  "conseil": "consulting",
  "partnaires": "partners",
  "a-propos": "about",
  "recherche": "search"
}
var url = window.location.href;
var urlRedirect = url.replace("/fr/", "/en/");
urlRedirect = urlRedirect.replace(!!) // how can I use the dec ?

For example if my URL like this: http://exemple.com/fr/agence 例如,如果我的网址是这样的: http : //exemple.com/fr/agence

It should be like this: http://exemple.com/en/agency 应该是这样的: http : //exemple.com/en/agency

Iterate over the keys of the object using the for...in loop, then check if the key string is present in the URL , if yes, then replace() it with the value of that key. 使用for...in循环遍历对象的键,然后检查URL是否存在键字符串,如果是,则将其replace()为该键的值。

See below, where I'm using a fake URL just for this example: 参见下文,仅在此示例中使用伪造URL:

 var dec = { "agence": "agency", "conseil": "consulting", "partnaires": "partners", "a-propos": "about", "recherche": "search", "fr":"en" } //var url = window.location.href; //let's fake a url here: var url = "http://exemple.com/fr/agence"; console.log("entry URL: ", url) for (var key in dec){ if (url.indexOf(key) > -1){ url = url.replace(key, dec[key]) } } console.log("output URL: ", url); 

Note: This will work, but if the URL has one of the words from dec in the main part of the URL, it will also change (for example: www.agence.com/fr will become www.agency.com/en ) So, if you want just the path, the part after the / to change, use window.location.pathname , then replace just that part. 注意:这将起作用,但是如果URL的主要部分中包含dec中的一个单词,则URL也会更改(例如: www.agence.com/fr将变为www.agency.com/en )因此,如果只想更改/后面的部分的路径,请使用window.location.pathname ,然后仅替换该部分。

You have to get the string after the last slash and replace it by whatever dec[string] is: 您必须在最后一个斜杠之后获取字符串,并用dec[string]替换它:

 var dec = { "agence": "agency", "conseil": "consulting", "partnaires": "partners", "a-propos": "about", "recherche": "search" } var url = "http://exemple.com/fr/agence" var urlRedirect = url.replace("/fr/", "/en/"); var positionOfLastSlash = urlRedirect.lastIndexOf('/'); var lastPart = urlRedirect.substring(positionOfLastSlash + 1); var firstPart = urlRedirect.substring(0, positionOfLastSlash+1); if (lastPart in dec) { console.log(firstPart+dec[lastPart]); } 

You could build a regex using Object.keys joining each key with | 您可以使用Object.keys|连接每个键来构建正则表达式| (or in a regex) then you could use it in the replace callback calling the value of dec . (或在正则表达式中),则可以在调用dec的值的replace回调中使用它。

 var dec = { agence: "agency", conseil: "consulting", partnaires: "partners", "a-propos": "about", recherche: "search" }; var url = "http://exemple.com/fr/agence/conseil/partnaires/a-propos/recherche"; var urlRedirect = url.replace("/fr/", "/en/"); var r = new RegExp(Object.keys(dec).join("|"), "gi"); urlRedirect = urlRedirect.replace(r, m =>dec[m]); console.log(urlRedirect); 

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

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