是否可以使用脚本将 URL 中的某个字符串更改为另一个术语?

Is it possible to change a certain string within an URL with another term using script?

大家好。

我有一个 tumblr 博客,它的导航是按标签组织的,形成 URL 这样的标签。

xxxx.com/home_english/

xxxx.com/home_spanish/

xxxx.com/news_english/

xxxx.com/news_english/chrono

xxxx.com/news_spanish/

xxxx.com/news_spanish/chrono

博客顶部有 2 个链接,允许用户更改博客的语言。但是,这些链接是静态的,始终指向所选语言的博客主页。

我的目标是允许用户在停留在当前正在访问的页面时随时更改语言,例如能够通过仅单击上述链接直接进入。 ..

xxxx.com/news_english/chrono 到...
xxxx.com/news_spanish/chrono。

我想脚本应该能够获取当前的 URL,找到与语言关联的字符串(总是在 URL 的唯一下划线之后,有时在域之后的第二个破折号之前,有时不是), 然后用目标语言替换它,最后重建 URL.

就可以了。非常感谢。

问候

按照 Simone 在他的评论中所说,从 url 创建一个字符串后,您应该找到 ${pageName}_${languageName} 单词;根据您提供的详细信息,您可以这样做:

// creating an string out of url; Note that you should define createUrlString function yourself
const urlString = createUrlString(url);

//finding the page_language word in the string
const wordsArray = urlString.split('/');
const pageLanguage = wordsArray.find(word => word.includes('_'))

完成此步骤后,您可以轻松更改 'pageLanguage' 单词,方法是将当前语言替换为所选语言,然后在 url 字符串中替换它。