use regex to find all words ending with "lex" in typescript

To find all words ending with "lex" in TypeScript, we can use the following regular expression:

index.ts
const str = "regex and typescript go hand in hand. complex regex patterns can be used for efficient string manipulation";
const pattern = /\b\w*lex\b/g;

const wordsEndingWithLex = str.match(pattern);
console.log(wordsEndingWithLex); // Output: ["regex", "complex"]
266 chars
6 lines

Here, \b represents the word boundary, \w matches any word character, and * represents zero or more occurrences. By wrapping the pattern with \b and using the global flag g, we can match all words that end with "lex" in the given string. The match() function returns an array of all matches found in the string.

gistlibby LogSnag