Philipp Lenssen
Philipp Lenssen

Reputation: 9218

Remove everything between "/*" and "*/" in JavaScript regex?

How would one remove all text between /* and */ (including these characters) in a string in JavaScript? Thanks!

Upvotes: 0

Views: 188

Answers (1)

Shef
Shef

Reputation: 45589

1. Your regex would be

/\/\*.*?\*\//g

2. The JS code to do the replacement would be:

your_str = your_str.replace(/\/\*.*?\*\//g, '');

3. Here is your explanation for the regex:

Match the character "/" literally
Match the character "*" literally
Match any single character that is not a line break character
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Match the character "*" literally
Match the character "/" literally

Upvotes: 5

Related Questions