Regex to Match Only Letters

Last Updated On Sunday 19th Dec 2021

Regular Expressions

  • Regular expressions are used in programming languages to match parts of strings.
  • You create patterns to help you do that matching.

Using the Test method and Match Method

  • The .test() method only returns a true or false response.
  • Using the .match() method instead will extract the matches.
  • The .match() method is used on Strings rather than Regex.
	function alphabetRegex(str) {
  const regex = /[a-z]/gi;
  const letters = str.match(regex);
  return letters.join(' ');
}

console.log(alphabetRegex('Hello')); // H e l l o
	

A collection of well-tested and well-performing regexes. You Can import from @terminus/ngx-tools/regex.

	npm I @terminus/ngx-tools
	

regex only letters

  • onlyLettersRegex: Regex requiring only English letters.
	import { onlyLettersRegex } from '@terminus/ngx-tools/regex';

onlyLettersRegex.test('Smi3e'); // false
onlyLettersRegex.test('Smile'); // true
	

Here is a great site to help practice and simply see what Regex is matching as you add onto an expression.