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

如何替换input中链接特定参数yy后的内容?技术咨询

How to Update the yy Parameter in a URL Inside an Input Field

Got it, let's fix this problem properly. First, a quick note: your original code uses returnlink.val which is a property reference—you need to call it as a method returnlink.val() to get or set the input's value. That's a common gotcha!

Here are two reliable ways to update just the yy parameter without replacing the entire URL:

This is the cleanest approach because it leverages browser-native APIs to handle URL parsing and modification, avoiding messy regex edge cases.

const yyNewval = '3xxxx';
// Grab the input element
const returnInput = $("input[name='return']");
// Get the current URL from the input
let currentUrl = returnInput.val();

// Parse the URL and update the yy parameter
const urlObject = new URL(currentUrl);
urlObject.searchParams.set('yy', yyNewval);

// Set the updated URL back into the input
returnInput.val(urlObject.href);

Why this works:

  • URL automatically parses the entire URL structure, including the query string.
  • searchParams.set() will either update an existing yy parameter or add it if it doesn't exist (though your use case says it's already present).
  • Handles all edge cases like yy being the first/last parameter, or having special characters in the value.

Method 2: Regular Expression (For Older Browser Compatibility)

If you need to support older browsers like IE11 that don't have URLSearchParams, a well-crafted regex will do the trick:

const yyNewval = '3xxxx';
const returnInput = $("input[name='return']");
let currentUrl = returnInput.val();

// Replace the yy parameter value while preserving the rest of the URL
const updatedUrl = currentUrl.replace(/(yy=)[^&]*/g, `$1${yyNewval}`);

// Update the input with the modified URL
returnInput.val(updatedUrl);

Regex breakdown:

  • (yy=): Captures the literal string yy= so we can reuse it in the replacement.
  • [^&]*: Matches any character that isn't an ampersand (the separator between parameters) until the end of the parameter value.
  • The g flag ensures we replace all occurrences of yy= (though you likely only have one).

Either method will get the job done, but URLSearchParams is the more robust choice for modern applications.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:50:08