With this pattern \bif\((.+)\)\{([\s\S]+?)} bif \bif\((.+)\)\{([\s\S]+?)} I select all the blocks if http://joxi.ru/8AnWw7VcqgpKOr how can I select all but these blocks. roughly speaking invert the pattern

I check in the service https://regexr.com/

  • It is possible so - s.split(/\bif\(.+\)\{[\s\S]+?}/) . Use regex101.com , everything is much clearer. - Wiktor Stribiżew
  • Check in the service, and share a screenshot ... There is also a share? - vp_arth
  • regex101.com/r/DW3W3I/1 - Dmitriy Rudnik

1 answer 1

Regular expressions are usually used to find text. For example, if you want to find all the digits, you can use /\d+/g . If you want to find all the characters other than numbers, you can use the "inverted" class \D , /\D+/g . For regular expressions containing sequences of patterns, there is no such construct that finds a text different from such a sequence (there is one such, Lucene, but it is rather an exception, and more like wildcard-patterns). The “greedy” “moderate” quantifier (tempered greedy token, /(?:(?!ШАБЛОН)[\s\S])+/g comes close in meaning, but it actually finds a sequence of characters that are not are the starting point of PATTERN.

To "negate" or "invert" this pattern, it is enough to split the string with its help, after replacing the capturing subtitles with non-catchable (to avoid adding the captured substrings to the resulting array):

 var s = "[prop_code_4] - это r('необычный', 'необыкновенный', 'особенный', 'феноменальный', 'незаурядный', 'особый', 'оригинальный', 'курьезный', 'непривычный', 'непростой', 'удивительный') гаджет, r('предназначенный', 'созданный', 'который создан') для погружения в мир r('виртуальной', 'дополненной', 'дополнительной') \r\nif( [prop_code_2] == 'Красный' ){\r\nСегодня для погружения в мир r('виртуальной', 'дополненной') r('реальности', 'действительности') r('достаточно', 'стоит только', 'необходимо') иметь при себе смартфон плюс [prop_code_4]. r('Обладая', 'Владея') всем этим, дело остается за малым, всего-то найти r('необходимый', 'нужный', 'подходящий') медиа-контент это могут быть как r('приложения, игры', 'игры, приложения'), так и r('видеоролики', 'видео', 'видеозаписи')\r\n}\r\n\r\n[prop_code_4] - это r('необычный', 'необыкновенный', 'особенный', 'феноменальный', 'незаурядный', 'особый', 'оригинальный', 'курьезный', 'непривычный', 'непростой', 'удивительный') гаджет, r('предназначенный', 'созданный', 'который создан') для погружения в мир r('виртуальной', 'дополненной', 'дополнительной') \r\nif( [prop_code_2] == 'Красный' ){\r\nСегодня для погружения в мир r('виртуальной', 'дополненной') r('реальности', 'действительности') r('достаточно', 'стоит только', 'необходимо') иметь при себе смартфон плюс [prop_code_4]. r('Обладая', 'Владея') всем этим, дело остается за малым, всего-то найти r('необходимый', 'нужный', 'подходящий') медиа-контент это могут быть как r('приложения, игры', 'игры, приложения'), так и r('видеоролики', 'видео', 'видеозаписи')\r\n}"; var result = s.split(/\bif\(.+\)\{[\s\S]+?}/); console.log(result.join("")); // если нужна строка console.log(result.filter(Boolean)); // если нужен массив 

.filter(Boolean) will remove empty array elements.