[英]Remove white-space after “/” in a string with RegEx
RegEx非常新
我需要删除字符串中的/
字符后的空格(如果存在)
防爆
查找/替换其他东西>>查找/替换其他东西
我正在尝试.replace(/[/]+$/g, "")
但它没有效果
我在Google Apps脚本中使用此功能
你可以匹配一个正斜杠( \\/
,转义,因为/
用作正则表达式分隔符char),然后任何一个或多个空格( \\s+
)替换为反斜杠:
s = s.replace(/\/\s+/g, '')
或者,使用捕获组(一对未转义的括号, (...)
)捕获正斜杠,并替换为替换反向引用$1
:
s = s.replace(/(\/)\s+/g, '$1')
看JS演示:
console.log("/abc/ spaces/ more spaces".replace(/\\/\\s+/g, '/')) console.log("/abc/ spaces/ more spaces".replace(/(\\/)\\s+/g, '$1'))
所有出现都被替换,因为使用了g
(全局)修饰符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.