1

I have a string with contents like this:

/*         
    <h1>some text</h1>             
    <div>more stuff $${}{} etc </div>        
*/

I'm trying to get everything between /* and */

Currently:

 string.match(/[^\/\*](.*)[(\*\/)$]/g);

Why doesn't this regex work?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
louisinhongkong
  • 551
  • 1
  • 6
  • 13

3 Answers3

3

Because the . does not match line breaks. Try this instead:

string.match(/\/\*([\s\S]*?)\*\//g);

I also made the * ungreedy otherwise the first /* and last */ would be matched. I.e., the entire input:

/*         
    <h1>some text</h1>             
    <div>more stuff $${}{} etc </div>        
*/
var somethingImportant = 42;
/*         
    ...     
*/

Realize that you will goof up on input like this though:

var s = " ... /* ... ";
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
1
[(\*\/)$]

This tells the regular expression to match any of these characters:

  • (
  • *
  • /
  • )
  • $

The first character class is similar.

I don’t really know what you were going for there, but this should work:

/\/\*([\s\S]*?)\*\//

\/\* matches /*; the group matches any number of anything, but as little of it as possible ([\s\S] becase . won’t match newlines in JavaScript and there isn’t an option); and \*\/ matches */.

Enjoy trying this out with '/* strings', /regular expressions/*5, // inline comments /*, and <!-- other inline comments /*.

Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Try \/\*((?:[^*]|(?:\*(?=[^\/])))*)

The result should be on the first match group.

In languages with zero length look behind (JavaScript does not support this) this should work:

(?<=\/\*)(?:[^*]|(?:\*(?=[^\/])))*

Basically it looks for anything after /* which is either not a * or a * not followed by '/' up until the first */

davidrac
  • 10,723
  • 3
  • 39
  • 71
  • thanks for the answer david, but Barts pattern seems to be working alright for now. – louisinhongkong Mar 18 '14 at 21:00
  • David, FYI: JavaScript does not support `(?<=\/\*)` (look behinds). Your answer suggests you know this, but that might not be known to people reading for an answer to their question in this Q&A. – Bart Kiers Mar 18 '14 at 21:01
  • Thanks Bart, this is why I wrote the first regex that should work in JavaScript and stated that the second will only work in a language with zero length look behind :) In any case I think the solution you wrote using the ungreedy match is more elegant. – davidrac Mar 18 '14 at 21:04