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

如何使用JavaScript替换getlink开头语句后的指定URL?

How to Replace Specific URL in String with JavaScript

Hey there! Let's solve this problem where we need to swap out the URL only after the "getlink file:" part in your string. Here are a couple of straightforward ways to do it:

Regex is perfect here because it lets us target exactly the part we want to replace without messing with the rest of the string. Here's how:

// Your original text
const originalString = "download file: xxxxxxxxxx.com getlink file: xxxxxxxxxx.com";

// Replace only the URL after "getlink file:"
const modifiedString = originalString.replace(/(getlink file: )\S+/, '$1mmmmmmm111.com');

console.log(modifiedString);
// Output: "download file: xxxxxxxxxx.com getlink file: mmmmmmm111.com"

Breakdown of the regex:

  • (getlink file: ): This is a capturing group that keeps the "getlink file: " part intact (we'll reuse it in the replacement).
  • \S+: Matches one or more non-whitespace characters (this targets the old URL).
  • $1: References the content from our capturing group, so we don't lose the "getlink file: " text when replacing.

If you have multiple "getlink file:" entries in the string and want to replace all of them, just add the global flag (g) to the regex:

const modifiedString = originalString.replace(/(getlink file: )\S+/g, '$1mmmmmmm111.com');

Alternative: String Manipulation (For Simple Cases)

If you prefer avoiding regex, you can split the string and rebuild it. This works well if the structure of your string is always consistent:

const originalString = "download file: xxxxxxxxxx.com getlink file: xxxxxxxxxx.com";
const parts = originalString.split("getlink file: ");
// parts will be ["download file: xxxxxxxxxx.com ", "xxxxxxxxxx.com"]

const modifiedString = `${parts[0]}getlink file: mmmmmmm111.com`;
console.log(modifiedString);

This splits the string at "getlink file: ", then combines the first part with the new URL. Note that this only works for a single "getlink file:" entry.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:19:15