Character Set
Learn how to create a precise RegEx using the character set.
Overview of the character set
The character set, as the name suggests, is a set of characters.
First, let’s recall the previous example in the wildcard character chapter, where you created a RegEx to match the text “Boat”, “Bent”, “Boot”, “Beat” and the RegEx was /B..t/
. This RegEx was not precise as it will also match texts like “Best”, “Belt” etc. Let’s take another set of text “boat”, “Boat”. To match these texts, you can use a case-sensitive flag. We can match them using a character set.
How the character set works
Character set property: From the given set of characters, the RegEx engine will select any of the characters that match with text (only one character at a time). Characters mentioned inside the square bracket are considered elements of the character set. For example, [abc]
is considered as a character set. When the RegEx engine starts searching for the pattern in the text, it will match the character present in the text with all the characters mentioned inside the character set.
Let’s try to understand this with an example. Suppose your ...