You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript函数如何将URL提取的小写路径段转换为大写返回?

你可以直接对提取到的路径分段调用JavaScript内置的toUpperCase()方法,该方法会将字符串中所有小写字符转换为大写,不影响原有的大写字符、数字和特殊符号。

调整后的代码如下:

function() {
  var pageUrl = window.location.href;
  return pageUrl.split("/")[3].toUpperCase();
}

如果需要兼容URL路径层级不足3级、提取结果为undefined的场景,可以加一层判断避免调用方法时报错:

function() {
  var pageUrl = window.location.href;
  const targetSegment = pageUrl.split("/")[3];
  return targetSegment ? targetSegment.toUpperCase() : "";
}

内容的提问来源于stack exchange,提问作者Mr. M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 05:54:02