最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Regex match reverse group in javascript - Stack Overflow

programmeradmin2浏览0评论

I want to match strings that don't have abc, def or ghi. The opposite is easy:

/(abc|def|ghi)/

How do I reverse that? I don't want to

/(^abc|^def|^ghi)/

because there's going to be more 'logic' in there. (If that's even what it does.)

How do I reverse the whole group match (or whatever it's called)?

(I'm trying to beat 5. on /)

I want to match strings that don't have abc, def or ghi. The opposite is easy:

/(abc|def|ghi)/

How do I reverse that? I don't want to

/(^abc|^def|^ghi)/

because there's going to be more 'logic' in there. (If that's even what it does.)

How do I reverse the whole group match (or whatever it's called)?

(I'm trying to beat 5. on http://regex.alf.nu/)

Share Improve this question edited Dec 20, 2013 at 19:35 Rudie asked Dec 20, 2013 at 18:53 RudieRudie 53.9k42 gold badges135 silver badges175 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

Use negative lookaead:

/^(?!.*?(abc|def|ghi)).*$/

You need to define the capture group including the start (^) and end of the string ($), or you'll end up with false positive matches:

/^((?!(abc|def|ghi)).)*$/

This will match:

  • bob
  • joe

This will not match:

  • abc
  • def
  • ghi
  • bobabc
  • abcjoe

See it in action here: http://regex101./r/yI3tF4

Solution to 5. on http://regex.alf.nu/ : ^((?!(.)(.)\3\2).)*$
Explanation : Regular expression to match a line that doesn't contain a word?

Using negative lookaheads :

/^(?!.*abc)(?!.*def)(?!.*ghi)/
/^(?!.*(abc|def|ghi))/
发布评论

评论列表(0)

  1. 暂无评论