Depending on what you want to continue with the extracted doc blocks, many different approaches come to mind. If you just need dockblocks without further reference, String.match () may be enough. Otherwise, you may need a block index.
As others have already pointed out, javascript RegEx is everything but powerful. if youโre used to PCRE, itโs like working with your hands behind your back. [\s\S] (space-character, non-space-character) is equivalent to dotAll - also captures line breaks.
This should help you:
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+/); };
source share