use regex to find words that end in "lex" in a string in typescript

You can use the following regular expression pattern in TypeScript to find words that end in "lex" in a given string:

index.ts
const str: string = "This text contains words like reflex, complex, and perplex";
const pattern: RegExp = /\b\w*lex\b/g;
const results: RegExpMatchArray | null = str.match(pattern);

if (results) {
  console.log(results); // ["reflex", "complex", "perplex"]
}
260 chars
8 lines

Here, \b represents a word boundary and \w represents a word character. The * ensures that any number of characters can come before "lex". The g flag is used to find all matches in the string.

This code will output ["reflex", "complex", "perplex"] in the console.

gistlibby LogSnag