Matching text between hyphens with regex

You can use the delimiters to help bound your pattern then capture what you want with parentheses.

/-([^-]+)-/

You can then trim the hyphens off.


You can use these regex

(?<=-).*?(?=-)//if lookaround is supported

OR

-(.*?)-//captured in group1

.*? matches any character i.e. . 0 to many times i.e. * lazily i.e ?

(?<=-) is a zero width look behind assertion that would match for the character - before the desired match i.e .*? and (?=-) is a zero width look ahead assertion that matches for - character after matching .*?

Tags:

Regex

Hyphen