如何通过两次拆分URL从外部链接中提取指定字段Name
Solution to Extract "Name" from Outgoing Link URLs
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
returnstatements 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?randomtextattached.
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
- Query parameter removal:
split("?")[0]ensures we get rid of any random text after the?, leaving us withhttps://www.example.com/Place/Name.html. - Grab last path segment:
split("/").pop()pulls the final part of the URL path — in your example, that'sName.html. - Remove file extension:
split(".html")[0]strips off the.htmlsuffix, 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?randomtextis:["https:", "", "www.example.com", "Place", "Name.html?randomtext"]
Sosplit("/")[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
相关产品推荐
相关产品推荐

