Home > front end >  Detect JavaScript comments starting with **! using RegExp
Detect JavaScript comments starting with **! using RegExp

Time:02-05

I have tried a lot doing this using RegExp.

Pattern: \*.*\*!

Test text:

/**!
 * Plugin Name 1.0.0
 * @author  Name
 */

/*! Test */

/* Test */

/************************************************************************/
/******/ /* Test */
/******/
/*!*********************!*\
  !*** ./index.ts ***!
  \*********************/

Link for test: https://regexr.com/6eoa0

It works great and matches the first comment and that's what I need, but for some reason, it matches the other two comments.

How can make it detect comments that start with **! (in one line).

CodePudding user response:

You can use

/\/\*\*![^*]*\* (?:[^\/*][^*]*\* )*\//g

See the regex demo. Details:

  • \/\*\*! - a /**! string
  • [^*]* - zero or more chars other than asterisks
  • \* - one or more asterisks
  • (?:[^\/*][^*]*\* )* - zero or more sequences of
    • [^\/*] - any char other than a / and *
    • [^*]* - zero or more chars other than asterisk
    • \* - one or more asterisks
  • \/ - a / char.
  •  Tags:  
  • Related