ParPar
ParPar

Reputation: 7569

Regex - Use /* in JS without getting my code in comment

I need to do some split, like this:

$(element).split(/*(.+)?/)[1]);

But those /* characters, make my code be as a comment. How can I use it & avoid from this to happen ?

Upvotes: 5

Views: 92

Answers (2)

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46738

Escape the * character to match it literally. It means something within a regex. It means the previous token is matched 0 or more times.

/\*

The regex should be:

/\*(.+)?/

Upvotes: 5

Joseph Adams
Joseph Adams

Reputation: 982

You could just add an expression to the start that doesn't make a difference:

/.{0,}*(.+)?/

Upvotes: 0

Related Questions