0

I'm looking to create a JS Regex that matches double spaces

([-!$%^&*()_+|~=`{}\[\]:";'<>?,.\w\/]\s\s[^\s])

The RegEx should match double spaces (not including the start or end of a line, when wrapped within quotes).

Any help on this would be greatly appreciated.

For example:

var x = 1,
    Y = 2;

Would be fine where as

var x =  1;

would not (more than one space after the = sign.

Also if it was

console.log("I am some console  output");

would be fine as it is within double quotes

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
Nick White
  • 1,602
  • 4
  • 20
  • 35

2 Answers2

4

This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."

We can solve it with a beautifully-simple regex:

(["'])  \1|([ ]{2})

The left side of the alternation | matches complete ' ' and " ". We will ignore these matches. The right side matches and captures double spaces to Group 2, and we know they are the right ones because they were not matched by the expression on the left.

This program shows how to use the regex in JavaScript, where we will retrieve the Group 2 captures:

var the_captures = []; 
var string = 'your_test_string'
var myregex = /(["'])  \1|([ ]{2})/g;
var thematch = myregex.exec(string);
while (thematch != null) {
    // add it to array of captures
    the_captures.push(thematch[2]);
    document.write(thematch[2],"<br />");    
    // match the next one
    thematch = myregex.exec(string);
}

A Neat Variation for Perl and PCRE

In the original answer, I hadn't noticed that this was a JavaScript question (the tag was added later), so I had given this solution:

(["'])  \1(*SKIP)(*FAIL)|[ ]{2} 

Here, thanks to (*SKIP)(*FAIL) magic, we can directly match the spaces, without capture groups.

See demo.

Reference

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
  • 1
    If the "`requirements are not entirely clear to [you]`", you should consider first to ask for clarification in the comments section rather than shallow answering. (Edit: the downvote was mine ;) – try-catch-finally Jun 29 '14 at 11:44
  • this seems to still match console.log('should allow multiple Spaces 1!') – Nick White Jun 29 '14 at 11:48
  • @NickWhite It's not about the match, but about the Group 2 capture. The code retrieves Group 2. This is a "SKIP" technique that matches what we don't want in order to capture what we do want. Will add explanation. Please see the sample code. – zx81 Jun 29 '14 at 11:51
  • The code does not work yet: you're creating `regex` but using `myregex`. And it's still not clear (obviously not only to me) what Nick White excpects. Tip: you may use head-controlled `while` to spare one `exec()`: `do { ... } while (...);` :) – try-catch-finally Jun 29 '14 at 12:02
  • @try-catch-finally `regex` vs. `myregex` = good catch, thank you. :) Have a look at the answer... If you still think it should be downvoted, you have amazingly high standards, and I'll need to go learn from your answers. :) – zx81 Jun 29 '14 at 12:07
  • I prefer to vote on the spirit of the answer, and not whether the answer fully compiles or is 100% optimized. If the regex or overall approach is correct, then downvoting is pedantically useless or uselessly pedantic (they are commutative properties in this case :D ). You can constructively comment without downvoting. – codenheim Jun 29 '14 at 12:09
-1

Simple solution:

/\s{2,}/

This matches all occurrences of one or more whitespace characters. If you need to match the entire line, but only if it contains two or more consecutive whitespace characters:

/^.*\s{2,}.*$/

If the whitespaces don't need to be consecutive:

/^(.*\s.*){2,}$/
Barmar
  • 741,623
  • 53
  • 500
  • 612