最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - How can I find multi-line JavaScript comment blocks using a regular expression? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to pull code ment blocks out of JavaScript files. I'm making a light code documentator.

An example would be:

/** @Method: setSize
 * @Description: setSize DESCRIPTION
 * @param: setSize PARAMETER
 */

I need to pull out the ments setup like this, ideally into an array.

I had gotten as far as this, but realize it may not handle new lines tabs, etc.:

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

(Okay, this seems like it would be simple, but I'm going in circles trying to get it to work.)

I'm trying to pull code ment blocks out of JavaScript files. I'm making a light code documentator.

An example would be:

/** @Method: setSize
 * @Description: setSize DESCRIPTION
 * @param: setSize PARAMETER
 */

I need to pull out the ments setup like this, ideally into an array.

I had gotten as far as this, but realize it may not handle new lines tabs, etc.:

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

(Okay, this seems like it would be simple, but I'm going in circles trying to get it to work.)

Share Improve this question edited Apr 9, 2012 at 9:03 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Feb 13, 2012 at 15:04 ted.goodridgeted.goodridge 1505 bronze badges 1
  • 2 I am not sure that regexp is the best tool to use for this one as you're dealing with multiple lines and parsing logic depends on whether it's the first/last/middle line... – Oleg Mikheev Commented Feb 13, 2012 at 15:09
Add a ment  | 

3 Answers 3

Reset to default 5

Depending on what you want to continue doing with the extracted docblocks, multiple approaches e to mind. If you simply need the docblocks without further references, String.match() may suffice. Otherwise you might need the index of the block.

As others have already pointed out, javascript's RegEx machine is everything but powerful. if you're used to PCRE, this feels like working with your hands tied behind your back. [\s\S] (space-character, non-space-character) is equivalent to dotAll - also capturing linebreaks.

This should get you started:

var string = 'var foo = "bar";'
    + '\n\n'
    + '/** @Method: setSize'
    + '\n * @Description: setSize DESCRIPTION'
    + '\n * @param: setSize PARAMETER'
    + '\n */'
    + '\n'
    + 'function setSize(setSize) { return true; }'
    + '\n\n'
    + '/** @Method: foo'
    + '\n * @Description: foo DESCRIPTION'
    + '\n * @param: bar PARAMETER'
    + '\n */'
    + '\n'
    + 'function foo(bar) { return true; }';

var docblock = /\/\*{2}([\s\S]+?)\*\//g,
    trim = function(string){ 
        return string.replace(/^\s+|\s+$/g, ''); 
    },
    split = function(string) {
        return string.split(/[\r\n]\s*\*\s+/);
    };

// extract all doc-blocks
console.log(string.match(docblock));

// extract all doc-blocks with access to character-index
var match;
while (match = docblock.exec(string)) {
    console.log(
        match.index + " characters from the beginning, found: ", 
        trim(match[1]), 
        split(match[1])
    );
}

This should grab a ment block \/\*\*[^/]+\/. I don't think Regexp is the best way to generate an array from these blocks though. This regexp basically says:

Find a /** (the asterisk and forward slashes are escaped with \)

then find anything that isn't a /

then find one /

It's crude but is should generally work. Here's a live example http://regexr.?300c6

What about some magic :)

ment.replace(/@(\w+)\s*\:\s*(\S+)\s+(\w+)/gim, function (match, tag, name, descr) {
    console.log(arguments);
    // Do sth. ...
});

I've not tested this so for the regex there is no guarantee, just to point you to a possibility do some RegExp-search the John Resig way 8-)

发布评论

评论列表(0)

  1. 暂无评论