An alphabet is a unit of information that represents a single letter i.e., from a-z or A-Z. A consonant is an alphabet that does not contain vowels i.e., all alphabets excluding a,e,i,o,u. In this article let's understand how we can create a regex for consonant and how regex can be matched for a given consonant.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of alphabets that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a consonant
The string should have the following criteria and structure-
- a consonant is a unit of information that represents a single letter from b-d, f-h, j-n, p-t, v-z and its capitalized letters
Regex for checking if consonants is valid or not
Regular Expression for a single consonant-
Regular Expression for one or more consonants-
Test string examples for the above regex-
|Input String||Match Output|
|12G||does not match|
|ATOZ||does not match|
|aeiou||does not match|
Here is a detailed explanation of the above regex-
/^[b-df-hj-np-tv-z]+$/gmi ^ asserts position at start of a line Match a single character present in the list below [b-df-hj-np-tv-z] + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy) b-d matches a single character in the range between b (index 98) and d (index 100) (case insensitive) f-h matches a single character in the range between f (index 102) and h (index 104) (case insensitive) j-n matches a single character in the range between j (index 106) and n (index 110) (case insensitive) p-t matches a single character in the range between p (index 112) and t (index 116) (case insensitive) v-z matches a single character in the range between v (index 118) and z (index 122) (case insensitive) $ asserts position at the end of a line Global pattern flags g modifier: global. All matches (don't return after first match) m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string) i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Hope this article was useful to check if the string is a valid consonants or not.