Regular expression question

Hi,

I’m looking at a way to strip comments out of a script text node.

This is where I’m at right now

var singleLineCom = /\\/{2}[\\s\\S]+?(?=\
)/g
var multiLineCom = /\\/\\*[\\s\\S]+?(?=\\*\\/)/g

An example of the multi line

/* Hopefully this script
will have the know how
to pick out various comments*/

/* and additonally these
following comments*/

Output
[“/* Hopefully this scrip…ck out various comments”, “/* and additonally these
following comments”]

As you can see I’m left with the starting /* and the newlines. I can obviously remove these on a separate pass, but would like to know if there’s a way to incorporate this into one regex.

I’ve tried /(?:\/\\s+)([\s\S]+?)(?=\\/)/ which get’s me closer, however as soon as I add the global modifier, the non-capture is ignored.

The single line similarly

Output
[“// like this one for instance”, “// Or this one”]

Cheers for any tips

RLM

Bit of a compromise

//head is my script text node

var multiCom = /\\/\\*\\s+?([\\s\\S]+?)(?=\\*\\/\
)/g
while ((coms = multiCom.exec(head)) != null){
	alert(coms[1].replace(/\
\	/g," "));
}

RPG

No answers. Never mind isn’t that important. Just practice, that’s all.

Working my way through the O’reilly’s Regular Expression cookbook, so hopefully will gain a bit of knowledge to come back to it later.

In the meantime went for something a bit like this, and combined the single and multi-line expressions.

var stripChars = /\	/g; // just tabs at this point
var regScript = /\\/{2}\\s*?([\\s\\S]+?)(?=\
)|\\/\\*\\s*?([\\s\\S]+?)(?=\\*\\/)/g;
.
.
.
.
while ((match = regScript.exec(node.innerHTML)) != null){
	i = (match[1])? 1 : 2;
	comments[node.nodeName].push(match[i].replace(stripChars," "));
}

RLM