Regex: match white spaces that are not enclosed in []

For example, for this line

div.img-wrapper img[title="Hello world"] 

I want to combine the first space, but not the second space (which is enclosed in []). What is a regular expression?

+4
source share
2 answers

The following expression will complete the task using the forward-looking statement.

 _(?>[^[\]]*(\[|$)) 

The underline is a space. This expression does not support nested parentheses because the regular expression is not powerful enough to express nested matched structures.

 _ Match the space and (?> assert that it is not inside brackets [^[\]]* by matching all characters except brackets ( followed by either \[ an opening bracket (a space inside brackets will have a closing bracket at this position) | or $ or no more characters (end of line). ) ) 

UPDATE

Here is another (and more beautiful) solution that uses a negative forecast statement.

 _(?![^[\]]*]) 

He claims that the next bracket after a space is not a closing bracket.

+4
source

Do not match (split?) A space. Instead, match it to negative.

 (?:(?:\[[^\]]*\])|\S)+ 

This is not intended to fit all CSS selectors, just your example. You should get a CSS parser for reliable results.

0
source

Source: https://habr.com/ru/post/1310667/


All Articles