Aliaksandr Sushkevich
Aliaksandr Sushkevich

Reputation: 12364

Grab all the whitespaces except ones inside double quotes

I need to select all the whitespaces except those ones inside double quotes " "

So for the input below, I need to select all the whitespaces except those ones inside "constant one two" - consider that this code sample is my whole input string:

function void test (  )   {   
    if  ( false )   { 
        let s  =  "constant one two" ; 
        let s  =  null ; 
        let a [ 1 ]   =  a [ 2 ]  ; 
     } 
 } 

I tried a lot of options with positive and negative lookahead but with no luck yet.

Upvotes: 0

Views: 105

Answers (1)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Use

\s(?=([^"]|"[^"]*")*$)

See proof. It matches any whitespace followed with zero or more characters other than double quote or a substring between double quotes.

Or, use PCRE pattern if possible:

"[^"]*"(*SKIP)(*F)|\s

See another proof. It matches any substring between quotes and drops the match, else, it matches whitespace, which will be outside of the quotes.

Upvotes: 1

Related Questions