albert
albert

Reputation: 23

regex extract string word

how can you extract field- * but other than field-req

input:

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

output:

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

I have this, but it does not meet

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

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

Upvotes: 2

Views: 51

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You may use

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

See the regex demo

Details

  • \b - a word boundary
  • field- - a literal substring
  • (?!req\b) - the must not be req and end of word boundary immediately to the right of the current location
  • \w+ - 1 or more word chars (replace with [a-zA-Z]+ to only match ASCII letters).

Upvotes: 3

Related Questions