Skip to content
Advertisement

regex not matching spaces on text between parentheses

Why doesn’t this regex match white space on text between parentheses? ((?:((s>)|(s{2,})|(s))))

The text below it that isn’t enclosed in parenthesis should be ignored. It’s just there. No need to worry with that. Only the ones in parentheses need to be touched.

JavaScript

Edit:

JavaScript

Advertisement

Answer

You can use

JavaScript

See the regex demo. Details:

  • (?:G(?!A)|() – either the end of the previous match or (
  • [^()]*? – zero or more chars other than (, ), as few as possible
  • K – omit the text matched so far
  • (?:(?<=()s+|s+(?=))|s{2,}) – 1+ whitespaces right after (, or 1+ whitespaces right before (, and any two or more whitespace characters
  • (?=[^()]*)) – that are followed with any 0 or more chars other than ( and ) and then a ).
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement