0

I am a newbie and have been struggling the last hour to figure this out. Let's say you have these strings:

baa cec haw heef baas bat jackaay

I want to match all the words which don't have two aa's consecutively, so in the above it will match cec, haw, heef, bat.

This is what i have done so far, but it's completely wrong i can sense :D

\w*[^\s]*[^a\s]{2}[^\s]*\w*
Alan Moore
  • 73,866
  • 12
  • 100
  • 156

3 Answers3

1

You maybe want to use negative lookahead:

/(^|\s)(?!\w*aa\w*)(\w+)/gi

You can check your string by paste this code on console on Chrome/Firefox (F12):

var pattern = /(^|\s)(?!\w*aa\w*)(\w+)/gi;
var str = 'baa cec haw heef baas bat jackaay';
while(match = pattern.exec(str))
    console.log(match[2]); // position 2 is (\w+) in regex

You can read more about lookahead here. See it on Regex101 to see how this regex work.

Community
  • 1
  • 1
Van M. Tran
  • 102
  • 1
  • 7
1

You need a regex that has 2 things: a word boundary \b and a negative lookahead right after it (it will be sort of anchored that way) that will lay restrictions to the subpattern that follows.

\b(?!\w*aa)\w+

See the regex demo

Regex breakdown:

  • \b - word boundary
  • (?!\w*aa) - the negative lookahead that will cancel a match if the word has 0 or more word characters followed by two as
  • \w+ - 1 or more word characters.

Code demo:

var re = /\b(?!\w*aa)\w+/gi; 
var str = 'baa cec haw heef bAas bat jackaay bar ha aa lar';
var res = str.match(re);
document.write(JSON.stringify(res));
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you so much. I will try hard to understand how this works, but it works and is exactly what i wanted to achieve. – Stefan Bratanov Nov 07 '15 at 17:07
  • Please see [*Word Boundaries*](http://www.regular-expressions.info/wordboundaries.html) and [*Lookahead and Lookbehind Zero-Length Assertions*](http://www.regular-expressions.info/lookaround.html) articles. The main pattern is `\w`(alphanumeric+underscore matching subpattern) that we match 1 or more times (`+`), only after a non-word character (not `[a-zA-Z0-9_]`) if it has no `aa` (since we check the word first with the lookahead if it has `aa` after zero or more word caracters (`\w*`)). – Wiktor Stribiżew Nov 07 '15 at 17:13
0

in javascript, you could use filter and regex invert ! a non-capturing group ?:.

var strings = ['baa','cec','haw','heef','baas','bat','jackaay'];
strings = $(strings).filter(function(index, element){
   return !/.*(?:aa).*/.test(element);                // regex => .*(?:aa).*
});
BenG
  • 14,826
  • 5
  • 45
  • 60