正则表达式提取字符串词

时间:2019-01-11 10:01:06

标签: javascript regex

如何提取field- *而不是field-req

输入:

form-field field-input field-req
form-field field-parents field-req
cell-header form-field field-input
form-field field-responsible field-req
form-field field-select cdsddsadsa
form-field field-select field-req
form-field field-textarea field-req
form-field field-textarea

输出:

field-input
field-parents
field-input
field-responsible
field-select
field-select
field-textarea
field-textarea

我有这个,但不符合

\bfield\-((?!.*req).*)\b

https://regex101.com/r/9y7pAV/1

1 个答案:

答案 0 :(得分:3)

您可以使用

/\bfield-(?!req\b)\w+/g

请参见regex demo

详细信息

  • \b-单词边界
  • field--文字子字符串
  • (?!req\b)-不能为req,并且字词边界的紧靠当前位置的右边
  • \w+-1个或更多单词字符(用[a-zA-Z]+替换,仅匹配ASCII字母)。