Running into an issue doing a regex replacement…
the pattern [ ]+$
works fine in the VSCode editor but not when I put it in code:
data = `
Hello
World
test
.
`
console.log(data.replace(/[ ]+$/, ""))
If we run that snippet we can see that the regex did not do the replacements
Fresh out of ideas what could be the problem
2
Answers
$
matches end of input string. You need to addm
flag to RegEx for$
match end of line instead:^
and$
anchor to the very start and end of the test string.You need to add the
m
flag, to make them work on a per line basis.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline#description: