简体   繁体   English

匹配多行文本中的字符串

[英]Matching strings in multiline text

I have the following string:我有以下字符串:

var str = "something\n
// comment\n
somethingElse();\n
end();\n";

I have to remove from it the commend and everything below it.我必须从中删除推荐及其下方的所有内容。 How can I do that?我怎样才能做到这一点? I tried:我试过:

str.replace(/(\/\/.*?end\(\);)/s, '');

But I am getting an error that the /s flag is undefined.但是我收到一个错误,即/s标志未定义。 What's wrong?怎么了?

str.replace(/\/\/.*\\n\r?\n/, "");

Matches two // + whatever followed by \\n and a unix or windows line-return.匹配两个// + 后跟\\n和 unix 或 windows 换行符的任何内容。

EDIT I think I misread your question.编辑我想我误读了你的问题。 To remove your comment and everything below it you can use要删除您的评论及其下方的所有内容,您可以使用

str.replace(/(\r?\n)\/\/[\s\S]*/, "$1");

but make sure the // start a new line.但请确保//开始一个新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM