NodeJS 如何从Javascript的字符串输入中只排除一些数字?[duplicate]

mefy6pfw  于 2023-01-16  发布在  Node.js
关注(0)|答案(1)|浏览(211)
    • 此问题在此处已有答案**:

How can I delete a query string parameter in JavaScript?(27个答案)
15小时前关门了。
所以我想弄清楚如何从字符串输入中排除一些数字。例如,如果我输入"www.example.com",我想删除"?variant = 41679925772494",并保持其余的不变。我不能使用常量"?variant = 41679925772494",因为我想让它对所有字符串输入都有效。https://www.shoepalace.com/products/jordan-dr6287-486-air-jordan-5-low-doernbecher-mens-lifestyle-shoes-blue-multi?variant=41679925772494", I'd want "?variant=41679925772494" remove and keep the rest the same. I can't use a constant of "?variant=41679925772494" because I'm wanting it work across all string inputs, not the same one. what I have is below, but it removes all numbers within the link, rather than whats after "?variant=". Does anyone have any ideas on how I can go about this?

let updatedLink = link.replace("?variant=", "")
let variantLink = updatedLink.replace(/[0-9]/g, "")```
31moq8wy

31moq8wy1#

如果你想解析URL,使用正则表达式并不是最好的选择。
浏览器有URL类,它完全符合您的要求。
例如

var n = new URL('https://www.shoepalace.com/products/jordan-dr6287-486-air-jordan-5-low-doernbecher-mens-lifestyle-shoes-blue-multi?variant=41679925772494');

n.searchParams.delete('variant');

console.log(n.toString());

相关问题