Nadav Tasher
Nadav Tasher

Reputation: 351

Regex only match specific whitespaces

I have some javascript code that i would like to minify using regex.

My goal is to turn this:

function mFunct(variable){
var a, b, c;
new Class();
}

Into this:

function mFunct(variable){var a,b,c;new Class();}

I have tried this regular expression:

(?:(new|var|function)\s)|\s

but it selects var a from the javascript code, which contains a crucial whitespace.

How can I change the regular expression so that it doesn't select it?

Upvotes: 2

Views: 67

Answers (1)

You can use the negative lookbehind to ensure matching only whitespace that is not preceeded by the keywords:

(?<!function|var|new)\s

See here. We can't see the newlines being highlighted, them having no width and all, but in the list on the right you see the characters being matched. Removing all those matched characters should give the desired output.

Upvotes: 1

Related Questions