pthorsson
pthorsson

Reputation: 341

Regex select everything except

I'm building a syntax highlighting script and i've got some problems with my regex. I've been failing for about 2 days now so i need help.

I want to select everything that not /* */ or between them and this is what i got atm, not working tho, but seems close:

^(?!(\/\*.*\*\/)$)

An example of i want:

/* I want to select everything but these comments */
.class-1 {
   font-family: 'SourceCodePro';
   font-size: 16px;
   line-height: 18px;
}
/* I want to select everything but these comments */
.class-2 {
   background-color: rgb(40, 40, 40);
   border: 1px solid rgb(20, 20, 20);
   padding: 10px 15px 10px 15px;
}

I've solved the other regex selections for the rest of the code, but since it's applying to the comments also i need to first select everything but them.

Upvotes: 0

Views: 140

Answers (4)

duckduckgo
duckduckgo

Reputation: 49

Take a look at this https://regex101.com/r/cN5aT1/2

/^(?!(\/\*(.+)\*\/)).*/gm

It is not working if comments are multiline

Upvotes: 1

Voonic
Voonic

Reputation: 4785

Removing comments | DEMO

For single // and multiline /* */

function filter(str)
{

    var regex =/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g;

    // if you want for multiline only then
    // use var regex =/\/\*.+?\*\//g;

    var temp = str.replace(regex,'');
    alert(temp);
} 

Upvotes: 0

unbreak
unbreak

Reputation: 990

Try this:

!(\/\*(.+)\*\/)

i cant check it now.

Upvotes: 1

Felix
Felix

Reputation: 38112

You can try to use this one:

!(/\*.+?\*/)

Upvotes: 1

Related Questions