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

如何用JavaScript的replace正则提取URL中动态变化的MYTEXT?

提取URL中动态MYTEXT内容的几种实用方法

下面是针对你的需求的几种可行实现方式,无需纠结动态内容的干扰:

方法一:用标准URL API解析(推荐)

利用浏览器或Node.js内置的URL API来解析路径,这种方式最规范,能适配URL路径的各种合法变化:

const targetUrl = "htts://query1.data.site.com/v7/finance/download/MYTEXT?period1=1664641287&period2=1696177287&interval=1d&events=history&includeAdjustedClose=true";
const urlObj = new URL(targetUrl);
// 将路径按斜杠分割,取最后一段就是目标内容
const myText = urlObj.pathname.split('/').pop();
console.log(myText); // 输出动态的MYTEXT值

方法二:正则表达式匹配

直接通过正则匹配download/之后、?之前的所有字符,精准定位目标内容:

const targetUrl = "htts://query1.data.site.com/v7/finance/download/MYTEXT?period1=1664641287&period2=1696177287&interval=1d&events=history&includeAdjustedClose=true";
const regex = /download\/([^?]+)/;
const matchResult = targetUrl.match(regex);
if (matchResult) {
  const myText = matchResult[1];
  console.log(myText); // 输出动态的MYTEXT值
}

方法三:字符串分割法(简单直接)

先截断URL的参数部分,再分割路径取最后一段:

const targetUrl = "htts://query1.data.site.com/v7/finance/download/MYTEXT?period1=1664641287&period2=1696177287&interval=1d&events=history&includeAdjustedClose=true";
// 先按?分割,拿到不带参数的路径部分
const pathWithoutParams = targetUrl.split('?')[0];
// 再按/分割,取最后一段
const myText = pathWithoutParams.split('/').pop();
console.log(myText); // 输出动态的MYTEXT值

内容的提问来源于stack exchange,提问作者cicoria

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 19:12:06