Guy Waldman Guy Waldman - 7 months ago 41
Javascript Question

Regex to match phrases not containing a palindrome

Is there a way to match a word not containing a palindrome (be it as long as it may)?

For instance, for a 6-character-long palindrome, foo/bar would match but xbarrabzz/1xoxxoxa14 would not match.


You can use \b(?:(?!(\w)(\w)\2?\1)\w)+\b.

Online Demo.

It's a simple negative lookahead that checks if the word contains a structure like xyx or xyyx.