javascript - 有没有办法删除两个斜杠内的所有字符?
问题描述
我正在尝试仅使用正则表达式来删除 '/please-remove-this/' 并将 '%20' 替换为 ' '。
let str = '/please-remove-this/Hello%20world'
let strNew = str.replace(/%20/g, ' ').substring(20)
strNew = 'Hello world'
'Hello world' 是正确的输出,但我觉得有一种更有效的方法可以只使用正则表达式
解决方案
而是替换%20
您可以使用解码decodeURI
let str = '/please-remove-this/Hello%20world';
let out = decodeURI(str.replace(/\/.*\//g, ''));
console.log(out)
let str = '/please-remove-this/Hello%20world';
let out = decodeURI(str.replace(/\/.*\/(.*)%20(.*)/, '$1 $2'));
console.log(out)
推荐阅读
- node.js - socket.io anonymous callback: can't call class functions
- java - Gradle 的“maven”和“maven-publish”插件有什么区别?
- angular - 使用代理解决 CORS 问题在浏览器中有效,而不是在模拟器或设备上
- rust - 尝试装箱特征对象时出现“预期的特征 A,找到 &A”
- java - Java - Random nextInt method returning the same integer
- excel-formula - Excel Conditionalis Statement to calculate the percentage value
- c# - How can I restrict an Interface to be implemented by only a generic type
- elixir - 渲染功能在视图中无法正常工作
- ios - How to set a specific CGPoint as the centre of UIImageView in Swift?
- delphi - Adding an "AddStream" method to TMultipartFormData for use with TNetHTTPClient.Post