我想转换一个字符串,它在另一个字符串中是等效的,它在人类可读中是等效的?
我从网址中获取参数:
bookmark/10/disco%20asdasd
所以我有"disco%20asdasd"的名字。但以这种方式它无法面对,所以我需要在"disco asdasd"中转换"disco%20asdasd",这是与 slugify 相反的操作。有人可以帮助我吗?
如果您显示要转换的字符串的示例和要将其转换为的字符串的示例,这将非常有帮助。没有这个,我只能向您推荐一些通用解决方案。看:
- https://www.npmjs.com/package/slug
- https://www.npmjs.com/package/slugify
- https://www.npmjs.com/package/slugg
- https://www.npmjs.com/package/clean-up-string
- https://www.npmjs.com/package/speakingurl
- https://www.npmjs.com/package/sluggin
- https://www.npmjs.com/package/simple-slug
- https://www.npmjs.com/browse/keyword/slug
请参阅这些项目文档中的代码示例。
更新
如果要将:disco%20asdasd
转换为:disco asdasd
,请使用:
let decoded = decodeURI(yourString);
或:
let decoded = decodeURIComponent(yourString);
但这不是一个 slug - 这是 URI 编码和解码。
看:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
关于维基百科的更多信息:
- https://en.wikipedia.org/wiki/Percent-encoding