Very new to RegEx
I need to remove whitespace after /
character in a string if it exists
Ex
find/ replace other stuff >> find/replace other stuff
I am trying .replace(/[/]+$/g, "")
but it has no effect
I am using this in a Google Apps Script
Very new to RegEx
I need to remove whitespace after /
character in a string if it exists
Ex
find/ replace other stuff >> find/replace other stuff
I am trying .replace(/[/]+$/g, "")
but it has no effect
I am using this in a Google Apps Script
You may match a forward slash (\/
, escaped since /
is used as a regex delimiter char) and then any 1 or more whitespaces (\s+
) to replace with a backslash:
s = s.replace(/\/\s+/g, '')
Or, capture the forward slash with a capturing group (a pair of unescaped parentheses, (...)
), and replace with a replacement backreference $1
:
s = s.replace(/(\/)\s+/g, '$1')
See the JS demo:
console.log("/abc/ spaces/ more spaces".replace(/\/\s+/g, '/'))
console.log("/abc/ spaces/ more spaces".replace(/(\/)\s+/g, '$1'))
All occurrences are replaced because g
(global) modifier is used.