0

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?

2 Answers 2

3

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()
}
Sign up to request clarification or add additional context in comments.

Comments

0

this should match multiline comments

var str = `/*
test
test
test
*/`

console.log(str.match(/\/\*([^]+)\*\//)[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.