5

I am in need of a Sublime Text 2 friendly regex that would allow me to search for all comments within a file.

The comments are all following this basic structure /* <anything> */

Thanks for taking the time to help :)

1

3 Answers 3

17

Search for:

(?s)/\*.*?\*/

This allows you to match comments that spreads over multiple lines.

The (?s) turns on "single line" mode, which makes . matches any character without exception (by default, . excludes line separators).

This assumes that there is no /* or */ inside a string literal.

Or if you want a bullet-proof solution, you may want to take a look at this question:

Can we make use of syntax highlighting feature to remove all comments from a source file in SublimeText?

Sign up to request clarification or add additional context in comments.

2 Comments

For a multiline match, this would be simpler: /\*[\s\S]+?\*/
@CAustin Note: If you use calc() and multiply inside or you're using sass and multiply in it or have comments that include * that solution doesn't work. At least didn't work for me.
14

Something like this should work:

\/\*.+?\*\/

I'm not super-familiar with Sublime Text, but this would work in Notepad++, and I believe the regex implementation is basically the same. If I'm wrong, feel free to let me know.

Edit: Per CAustin's helpful tip, you can also just do this (without the escaping of the forward slashes):

/\*.+?\*/

3 Comments

You don't need to escape the forward slashes, so this would work too: /\*.+?\*/
@CAustin Good to know; I will tweak accordingly.
\/\*[\s\S]+?\*\/ also captures multiline comments. @CAustin mentioned it in the comments of another answer, but I spent some minutes on regexr.com to figure it out because I didn't look further than the accepted answer. To save anyone from the same fate, I thought I'd double post that here -.-
0

I think this code is better:

\/\*[^(?:\*\/)]*\*\/

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.