Skip to content

Instantly share code, notes, and snippets.

@westc
Last active February 12, 2024 17:18
Show Gist options
  • Save westc/efa315de762da96b98e2 to your computer and use it in GitHub Desktop.
Save westc/efa315de762da96b98e2 to your computer and use it in GitHub Desktop.
getComments(fn, opt_trim) returns an array of all the comments in the specified function.
(function(RGX_COMMENT, RGX_TRIM) {
getComments = function (fn, opt_trim) {
var comments = [];
(fn + '').replace(RGX_COMMENT, function(m, a, b, c) {
if (!c) {
m = a == undefined ? b : a;
comments.push(opt_trim ? m.replace(RGX_TRIM, '') : m);
}
});
return comments;
};
})(/\/\*([\s\S]*?)\*\/|(?:<\!--|\/\/)([^\r\n]*)|(["'])(?:[^\\\3]+|\\[\s\S])+\3/g, /^[\s\xA0]+|[\s\xA0]+$/g);
@westc
Copy link
Author

westc commented Aug 29, 2014

The regular expression includes the part for match strings to avoid pulling what looks like a comment out of a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment