正则表达式帮助:多行模式 [英] Regular Expression help: multi-line mode
问题描述
我希望从字符串中删除最后一个换行符(如果有的话)。
这是我开始的地方:
var text =" hello \ n" ;;
text = text.replace(/^(.*)\\\
?$ /," $ 1");
这似乎有效,但如果测试中嵌入换行符则不行:
text =" hello \\\
good-bye \ n \\ nn;
我已经尝试了一些变化,但到目前为止没有运气。
(我通过测试
最后一个字符==''\ n'来解决问题'如果是这样的话,请使用text.slice(0,-1)
删除它。)
有人可以为我提供正确的正则表达式吗? br $> b $ b谢谢!
-Wayne
/,"
1" );
这似乎有效,但如果测试中嵌入换行符则不行:
text =" hello \\\
good-bye \ n" ;
到目前为止,我尝试了一些变化但没有运气。
(我通过测试来解决问题,如果
最后一个字符==''\ n''如果是这样,请使用text.slice(0,-1)
删除它。)
有人可以为我提供正确的正则表达式吗?
谢谢!
-Wayne
Wayne写道:
我希望从字符串中删除最后一个换行符(如果有的话)。
do
string = string.replace(/ \ n
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\ngood-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
/, "
1" );
This seems to work, but not if the test has embedded newlines:
text = "hello\ngood-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
Wayne wrote:I wish to remove the last newline (if any) from a string.The do
string = string.replace(/\n
这篇关于正则表达式帮助:多行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!