Unable to block comment a small piece of javascript code with regex

107 Views Asked by At

This is the line which works perfectly fine.

'some words'.split(/,*/);

But when I do a block comment as show below, I get a syntax error.

/* 'some words'.split(/,*/); */

Couldn't find about this behaviour anywhere. Not sure if this is a know bug in the language.

2

There are 2 best solutions below

0
Scimonster On

The */ in your regex gets parsed as the end of the comment, which is causing the syntax error.

Can you use a single-line comment instead?

// 'some words'.split(/,*/);
0
Bergi On

This is not a bug in the language, the */ is a known (and deliberate) overlap between regular expression literals and block comment delimiters in the grammar.

You can either use a single-line comment

// 'some words'.split(/,*/);

or change your regex

/* 'some words'.split(/,{0,}/);
'some words'.split(/,*(?:)/); */