Reputation: 2898
I would like to get the string contents within a multi line comment using JavaScript:
"/*
test
test
test
*/"
Given the above string, I would like the contents, test test test
.
I've searched SO and cannot find a suitable answer. Does anyone know how to get all of the contents of a string within a multi line comment?
Upvotes: 0
Views: 58
Reputation: 635
this should match multiline comments
var str = `/*
test
test
test
*/`
console.log(str.match(/\/\*([^]+)\*\//)[1]);
Upvotes: 0
Reputation: 3361
you can use a regular expression to find and match the contents
/\/\*([\s\S]*?)\*\//
the expressions will try to close on the first */
. be aware that if comments are closed in comments, this might be different from how parsers handle this. in that case try fiddling with the lazy modifier (?
).
as js:
var str = "/*\ntest\ntest\ntest\n*/"
var match = str.match(/\/\*([\s\S]*?)\*\//)
if ( match ) {
var commentContent = match[1].trim()
}
Upvotes: 3