JAVASCRIPT
Extract All Alphanumeric Words from Text
Learn to extract all sequences of alphanumeric characters (words) from a given text string using JavaScript's `match` method and regular expressions. Ideal for text analysis and tokenization.
function extractWords(text) {
const wordRegex = /\b[a-zA-Z0-9]+\b/g;
return text.match(wordRegex) || [];
}
// Examples
console.log(extractWords("Hello, world! This is a test with 123 numbers.")); // ["Hello", "world", "This", "is", "a", "test", "with", "123", "numbers"]
console.log(extractWords("Hyphenated-word and 'quoted' text.")); // ["Hyphenated", "word", "and", "quoted", "text"]
console.log(extractWords("Special $$$ characters & symbols.")); // ["Special", "characters", "symbols"]
How it works: This JavaScript function extracts individual alphanumeric 'words' from a string. The regex `\b[a-zA-Z0-9]+\b` uses `\b` for word boundaries and `[a-zA-Z0-9]+` to match one or more letters or numbers, effectively separating words from punctuation and spaces. This is useful for natural language processing or searching.