Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:03:07+00:00 2026-06-06T08:03:07+00:00

I am trying to write a regex (in JavaScript) that will match a multi

  • 0

I am trying to write a regex (in JavaScript) that will match a multi line comment at the beginning of a JS file.

So far, I came up with this: /^(\/\*[^\*\/]*\*\/)/g

It works for a single line comment: https://regex101.com/r/ZS5PVI/1

But my problem is that it does not work for a multi line comment: https://regex101.com/r/ZS5PVI/2

Do you have any ideas how to solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:03:08+00:00Added an answer on June 6, 2026 at 8:03 am

    Like HTML, JavaScript cannot be parsed by regular expressions. Attempting to do so correctly is futile.

    Instead, you must use a parser that will correctly transform JavaScript source code into an AST, which you may inspect programmatically. Fortunately, there’s libraries that do the parsing for you.

    Here’s a working example that outputs the AST of this code:

    /* this is a
    multi-line
    comment */
    
    var test = "this is a string, /* and this is not a comment! */";
    
    // ..but this is
    

    Which gets us:

    [
      "toplevel",
      [
        [
          {
            "name": "var",
            "start": {
              "type": "keyword",
              "value": "var",
              "line": 5,
              "col": 4,
              "pos": 57,
              "endpos": 60,
              "nlb": true,
              "comments_before": [
                {
                  "type": "comment2",
                  "value": " this is a\n    multi-line\n    comment ",
                  "line": 1,
                  "col": 4,
                  "pos": 5,
                  "endpos": 47,
                  "nlb": true
                }
              ]
            },
            "end": {
              "type": "punc",
              "value": ";",
              "line": 5,
              "col": 67,
              "pos": 120,
              "endpos": 121,
              "nlb": false,
              "comments_before": []
            }
          },
          [
            [
              "test",
              [
                {
                  "name": "string",
                  "start": {
                    "type": "string",
                    "value": "this is a string, /* and this is not a comment! */",
                    "line": 5,
                    "col": 15,
                    "pos": 68,
                    "endpos": 120,
                    "nlb": false,
                    "comments_before": []
                  },
                  "end": {
                    "type": "string",
                    "value": "this is a string, /* and this is not a comment! */",
                    "line": 5,
                    "col": 15,
                    "pos": 68,
                    "endpos": 120,
                    "nlb": false,
                    "comments_before": []
                  }
                },
                "this is a string, /* and this is not a comment! */"
              ]
            ]
          ]
        ]
      ]
    ]
    

    Now it’s just a matter of looping over the AST and extracting what you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a regex that will match everything BUT an apostrophe that
I'm trying to write a regex expression that will take this phrase [[32, 120],
I'm trying to write a regex to match any path that contains /? to
I have been trying to write a regex that will remove whitespace following a
I am trying to write a regex that will allow me to parse CSV
I'm trying to write a regex pattern that will find numbers with two leading
I'm trying to write a regex to match entries in delimited files that have
I'm trying to write a Javascript regex that matches words with less than 3
I'm trying to write a regex that will remove HTML tags around a placeholder
I'm trying to write a regex to match patterns like this: <td style=alskdjf />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.