正则表达式匹配除给定单词之外的所有内容(可能包括连字符/破折号)

我想匹配除给定单词之外的所有内容,因此给出以下列表:
wordOne
wordTwo/xy/z
word-three
word-four/lots/of/stuff

我可以使用这个正则表达式匹配除wordOne之外的所有内容:

(?!wordOne)\b.+
  ==>
    wordTwo/xy/z
    word-three
    word-four/lots/of/stuff

但是,如果我想匹配除了包含连字符/破折号的单词之外的所有内容,则相同的正则表达式不起作用,因为连字符不是单词边界的一部分 – 这是[a-zA-Z0-9_]

例如

some-regexp(word-four)
  ==>
    wordOne
    wordTwo/xy/z
    word-three

some-regexp(word-four and word-three)
  ==>
    wordOne
    wordTwo/xy/z
我可以看到你在示例中每行定义一个单词.在这种情况下,这个正则表达式应该适合你:
^(?:(?!word-four|word-three).)*$

它会跳过包含四字或三字的单词.

根据你的例子:

^(?:(?!wordOne).)*$
  ==>
    wordTwo/xy/z
    word-three
    word-four/lots/of/stuff 

^(?:(?!word-four).)*$
  ==>
    wordTwo/xy/z
    word-three
    word-four/lots/of/stuff 

^(?:(?!word-four|word-three).)*$
  ==>
    wordOne
    wordTwo/xy/z

rubular上看到它.

相关文章

jquery.validate使用攻略(表单校验) 目录 jquery.validate...
/\s+/g和/\s/g的区别 正则表达式/\s+/g...
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母...
this.optional(element)的用法 this.optional(element)是jqu...
jQuery.validate 表单动态验证 实际上jQuery.validate提供了...
自定义验证之这能输入数字(包括小数 负数 ) <script ...