Guy Waldman Guy Waldman - 1 year ago 78
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download