chingis
chingis

Reputation: 1770

How to match regex if prefix is the same as suffix

I have string asdf-zxcv-qwer-

and I want to replace all occurence of words between - I'm using /-[a-z]+-/ It match only zxcv, because hyphen before qwer is used as prefix after zxcv. How to make it match both zxcv and qwer?

Upvotes: 1

Views: 774

Answers (1)

Gumbo
Gumbo

Reputation: 655129

Use an assertion, either look-behind ((?<=…)) or look-ahead assertion ((?=…)), which is checked but not consumed:

/(?<=-)[a-z]+-/
/-[a-z]+(?=-)/

Upvotes: 3

Related Questions