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

如何通过两次拆分URL从外部链接中提取指定字段Name

Let's break down what's wrong with your original code and fix it step by step:

Issues with your current code

  • You have two return statements back-to-back — the second one (return Name.split("?")[0];) will never run, since the first return exits the function immediately.
  • The order of operations is off: you're splitting the URL by / first before handling the query parameters, which means you're trying to work with a segment that still has ?randomtext attached.

Correct Implementation

Since your target URL structure is https://www.example.com/Place/Name.html?randomtext, here's how to reliably extract the "Name" part only for outgoing links:

function() { 
  // Only run logic if this is an outgoing link
  if ({{outgoing link}}) {
    const clickUrl = {{Click URL}};
    
    // Step 1: Strip off query parameters (everything after "?")
    const urlWithoutQuery = clickUrl.split("?")[0];
    
    // Step 2: Split the clean URL by "/" and grab the last segment (e.g., "Name.html")
    const lastPathPart = urlWithoutQuery.split("/").pop();
    
    // Step 3: Remove the ".html" suffix to get pure "Name"
    const name = lastPathPart.split(".html")[0];
    
    return name;
  }
  
  // Return empty string or default value if not an outgoing link
  return "";
}

How this works

  1. Query parameter removal: split("?")[0] ensures we get rid of any random text after the ?, leaving us with https://www.example.com/Place/Name.html.
  2. Grab last path segment: split("/").pop() pulls the final part of the URL path — in your example, that's Name.html.
  3. Remove file extension: split(".html")[0] strips off the .html suffix, leaving you with exactly the "Name" value you need.

Quick note on your "Place" extraction

Your existing code for "Place" works because when you split the full URL by /, the 3rd index (arrays start at 0) corresponds to "Place":

  • Split result of https://www.example.com/Place/Name.html?randomtext is:
    ["https:", "", "www.example.com", "Place", "Name.html?randomtext"]
    So split("/")[3] would get you "Place" — looks like your variable name was just a small typo there, but the core logic for that field was on track!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 21:27:43