Regex match words

Words match

This regular expression can be used to validate that a given string contains only characters in it or extract two words from a given string.

Simple word match

The regular expression to match only words looks like this (including compound words):

/^\b(?:\w|-)+\b$/

Test it!
/^\b(?:\w|-)+\b$/

True

False

Enter a text in the input above to see the result

Example code in Javascript:

var wordsRegex = /^\b(?:\w|-)+\b$/;
// Validate words
wordsRegex.test('word'); // Returns true
wordsRegex.test('pet-friendly'); // Returns true
wordsRegex.test('not a word'); // Returns false

// Extract words from a string
var wordsRegexG = /\b(?:\w|-)+\b/g;
'Hello, world!'.match(wordsRegexG); // returns ['Hello', 'world']
Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result