I wish to remove the last newline (if any) from a string.
This is where I started:
var text = "hello\n";
text = text.replace( /^(.*)\n?$/, "$1" );
This seems to work, but not if the test has embedded newlines:
text = "hello\ngoo d-bye\n";
I've tried a few variations but no luck so far.
(I worked around the problem by testing if the
last character == '\n' and if so use text.slice(0,-1)
to remove it.)
Can someone provide me the proper regex for this?
Thanks!
-Wayne
This is where I started:
var text = "hello\n";
text = text.replace( /^(.*)\n?$/, "$1" );
This seems to work, but not if the test has embedded newlines:
text = "hello\ngoo d-bye\n";
I've tried a few variations but no luck so far.
(I worked around the problem by testing if the
last character == '\n' and if so use text.slice(0,-1)
to remove it.)
Can someone provide me the proper regex for this?
Thanks!
-Wayne
Comment