从 URL 解码 %20 时出现问题

Issue with Decoding %20 from URL

我发现以下线程提供了一种非常有用的方法来创建永久链接或通过 URL 传递字符串值:

Original Thread

不幸的是,如果您想通过 URL 将字符串 "test string" 传递给特定的 <div> 并将其显示为简单文本,上述线程不会如果您的 URL 看起来像这样,似乎无法解码白色 space:

http://www.abc123.org/subpage.html?test%20string

该代码将简单地获取 URL 中的任何内容并传递给“?”它将显示为 "simple%20text"。

有没有一种简单的方法可以做类似于Thread's accepted answer的事情,以便所有 %20 都可以替换为白色 space?谢谢!

您可以使用 decodeURI():

Replaces each escape sequence in the encoded URI with the character that it represents, but does not decode escape sequences that could not have been introduced by encodeURI. The character “#” is not decoded from escape sequences.

const result = decodeURI('http://www.abc123.org/subpage.html?test%20string');

console.log(result);