Brian
Brian

Reputation: 7654

Properly match every line of JS multiline comment

// I have this string:

var a = "/**  \n" + 
        "  * foo bar  \n" + 
        "  * baz bob  \n" + 
        "  *  \n" + 
        "  * Yes, it is a string containing a docstring  \n" + 
        "  */  ";

// Having some experience in regex, I wrote a JavaScript regex expression:

var r = /\/\*\*\s*(.*\s*)*\*\//g;

// That regex successfully captures, as expected,

// but only the last line of the docstring in its second capture:

var match = r.exec(a.toString());  console.log(match);

// ["/**
//  * foo bar
//  * baz bob
//  * Yes, it is a string containing a docstring
//  */", 
//  "* Yes, it is a string containing a docstring"
// ]

// In order to capture all lines in this "string" similar to match[1], what am I still missing?

// This question is its own fiddle.

Upvotes: 3

Views: 74

Answers (1)

anubhava
anubhava

Reputation: 785128

The dotall flag, which in most languages is the s modifier, does not exist in Javascript.

You can work around this by using \s and its negation \S together in a character class. [\S\s]

var r = /\/\*\*\s*([\S\s]*)\*\//g;

Upvotes: 3

Related Questions