Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for hyphenated words

I have hyphen separated words in paragraphs that I'm trying to extract.

I've tried following regex \w+-\w, however this is not working as expected.

Here's the complete code written in JavaScript.

var string = "time to eval-u-ate";
var result = string.match(/\w+-\w/g); // ["eval-u"]

This returns the string eval-u. I want the result to be eval-u-ate. How can I modify the regex to match complete hyphenated words.

like image 685
Deke Avatar asked Feb 08 '23 06:02

Deke


1 Answers

You can use following regex

((?:\w+-)+\w+)
  1. (?:\w+-)+: Matches one or more alphanumeric characters including underscore symbol followed by a hyphen. (?: will make it not add in captured group
  2. \w+: Matches one or more alphanumeric characters including underscore symbol
  3. (): Capturing group. The matches can be accessed by using $n where n is the capturing group number. $1 in this case as it is first capturing group.
  4. g: Use global flag to get all possible matches

Demo

var string = "time to eval-u-ate Lorem ipsum dolor sit amet, consectetur adipisicing elit. A sed, illum veritatis aut recusandae tempora possimus iure totam distinctio necessitatibus temporibus labore-numquam-dignissimos, officiis velit error-dolores nostrum ipsam.";
var matches = string.match(/((?:\w+-)+\w+)/g);

document.write('<pre>' + JSON.stringify(matches, 0, 4) + '</pre>');

Regex101 Demo

like image 119
Tushar Avatar answered Feb 11 '23 01:02

Tushar