Overview of Literal Characters

Let’s deep dive into literal pattern matching.

Property of Literal Character: Matches all the characters with the same properties in the same way.

To match texts as they are, we generally use literal characters.

An example to match alphabets

For example, to match all the words containing “cat”, the RegEx will be /cat/. Remember, by default, RegEx will only display the first result. To match all instances of /cat/, use the global flag (g). We can also create a RegEx consisting of a single character. Suppose you want to match each occurrence of the number “1”, then the RegEx will be /1/.

Suppose, you are provided with the text “cat, catastrophic, catalyst”. It will start by matching the character “c” from RegEx with the character “c” from the text, which will match. It will then move to the next character. It will match the character “a” and then the character “t”. The complete pattern is matched against the text. The answer will be returned. If the global flag is mentioned, the search will start again from the next character “,” of the text and the character “c” from RegEx, which will not match. Since the text is not over, it will move to the next character in the text. The next character is space " " which will also not match. Similarly, it will match all the characters one by one and return the answer in the delimited form.

Get hands-on with 1200+ tech skills courses.