Category: Regular expressions

  • Multiline Mode of Anchors ^ $, Flag “m”

    In this chapter, you will learn when and how to use the multiline mode of the anchors in JavaScript. The multiline property is a read-only property of an individual regexp instance. It specifies whether or not the m flag is applied inside the regexp. So, for enabling the multiline mode the m flag is used. The value of the multiline property is boolean:…

  • Anchors: string start ^ and end $

    In JavaScript, the anchors are the caret ^ and the dollar $ characters, having a specific meaning in a regular expression. The caret corresponds at the beginning of the text and the dollar- at the end. For example, let’s check whether the text begins with Welcome: The pattern ^Welcome means that the string starts and then Sarah. In another example, let’s check whether the text ends with…

  • Unicode: flag “u” and class \p{…}

    JavaScript applies Unicode encoding for strings. The majority of the characters are encoded with two bytes, but it allows representing at most 65536 characters. However, the range is not large enough for encoding all the possible characters. For that reason, several rare characters are encoded with four bytes. The Unicode values for some characters are represented below: Character…

  • Character Classes

    Character classes are used for distinguishing characters like distinguishing between digits and letters. Let’s start from a practical case. Imagine you have a phone number like +3(522) -865-42-76, and wish to turn it into pure numbers (35228654276). To meet that goal, it is necessary to find and remove everything that’s not a number.Character classes are…

  • Patterns and Flags

    Regular expressions are the patterns, providing a robust means of searching and replacing in the text. They are available via the Regexp object in JavaScript. Also, they are integrated into the methods of strings. Regular Expressions A regular expression ( or a “regexp”, “regex”, “reg”) encompasses a pattern and optional flags. Two syntaxes (long and short) are used for…