Exclude whitespace that is only comprised of tab characters in RegEx Pattern

Viewed 94

I'm trying to capture whitespace characters when there are two or more of them in a row, as well as individual, non-breaking spaces. My RegEx pattern is as follows and currently achieves this:

/((\s| ){2,}| )/g

I would like to alter this pattern so that if two or more whitespace characters are only comprised of the tab \t character, then they are ignored. How would I do this?

Examples:

'\t\t' needs to be ignored
' \t' needs to be captured
'\t ' needs to be captured
' ' needs to be ignored
'\t' needs to be ignored
' \t' needs to be captured
'  ' needs to be captured
' ' needs to be captured
1 Answers

You may use this regex:

(( |\t| )*(?: )+[ \t]*| +[ \t]+|\t+ +[ \t]*)

RegEx Demo

RegEx Details:

  • [ \t]*(?: )+[ \t]*: Match 1+   surrounded by 0 or more space/tab characters
  • +[ \t]+: Match 1+ space followed by 1+ space or tabs
  • \t+ +[ \t]*: Match 1+ tabs followed by 1+ space and then 0 or more space/tabs
Related