-1

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. What's wrong?

khernik
  • 2,059
  • 2
  • 26
  • 51
  • The `/s` flag is undefined :) I think you need `/m` for do multiline (I edited 3 times to find the correct flag out of 4 possible flags) – A wild elephant Oct 01 '15 at 15:06
  • Check the MDN documentation : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace –  Oct 01 '15 at 15:08
  • Related : http://stackoverflow.com/questions/1068280/javascript-regex-multiline-flag-doesnt-work – PinkTurtle Oct 01 '15 at 15:12

1 Answers1

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

Matches two // + whatever followed by \n and a unix or windows line-return.

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.

PinkTurtle
  • 6,942
  • 3
  • 25
  • 44