我对正则表达式非常陌生
如果字符串中存在,则需要删除/
字符后的空格
例如
find/ replace other stuff >> find/replace other stuff
我尝试使用.replace(/[/]+$/g, "")
,但没有效果
我正在使用Google Apps Script
我对正则表达式非常陌生
如果字符串中存在,则需要删除/
字符后的空格
例如
find/ replace other stuff >> find/replace other stuff
我尝试使用.replace(/[/]+$/g, "")
,但没有效果
我正在使用Google Apps Script
您可以匹配一个前斜杠(\/
,因为/
用作正则表达式分隔符而进行了转义),然后匹配任何1个或多个空格(\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
,所以所有出现的内容都会被替换。