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.
You can use following regex
((?:\w+-)+\w+)
(?:\w+-)+
: Matches one or more alphanumeric characters including underscore symbol followed by a hyphen. (?:
will make it not add in captured group\w+
: Matches one or more alphanumeric characters including underscore symbol()
: 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.g
: Use global flag to get all possible matchesDemo
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With