Category: 12. Regular Expressions

  • Methods of RegExp and String

    On this page, you will see how various methods work in-depth with regular expressions in JavaScript. Particularly, we will cover methods such as str.match(regexp), str.matchAll(regexp), str.split(regexp|substr, limit), str.search(regexp), str.replace(str|regexp, str|func), regexp.exec(str), and regexp.test(str). str.match(regexp) The str.match(regexp) method detects matches for regexp in the string str. This method includes the following three modes: In case there is no g flag in the regexp, it returns the initial match as an array with the properties index, input, as…

  • Sticky flag y, searching at position

    Let’s explore the process of search with the help of the y flag. It helps to implement the search at a particular position in the source string. For a better understanding of what it is, let’s start from a practical case. One of the most frequent tasks of the regular expressions is “lexical analysis”. For example, HTML includes attributes and tags, JavaScript contains variables, functions, and more.…

  • Catastrophic Backtracking

    At first sight, some regular expressions seem simple but may take too much time to execute. That can even make the engine of JavaScript to “hang”. In such cases, the browser suggests killing the script and reloading the page. But, it’s not a good idea. for JavaScript engine it can become a fragility. In this chapter, we…

  • Lookahead and Lookbehind

    Sometimes it is necessary to detect merely those matches for a pattern that are preceded and followed by another pattern. Specific syntaxes are used to meet that goal. They are known as lookahead and lookbehind. Together they are called lookaround. As a rule, lookaround corresponds to characters, giving up the match and returning only the…

  • JavaScript Alternation (OR)

    This chapter is dedicated to another term in JavaScript regular expressions: Alternation. The simple form of alternation is known as OR. It is signified by a vertical line character | in a regular expression. Let’s consider an example where it is necessary to find the following programming languages: HTML, PHP, Java, or JavaScript. The matching regexp will look as follows:html|php|java(script)?.…

  • Backreferences in pattern: \N and \k

    The contents of capturing groups can be used not only in the replacement string or in the result, but also the pattern. In this chapter, we will explore backreference in the pattern such as \N and \k<name>. Backreference by number: \N In the pattern, a group can be referenced by using \N ( it is the group number). To make it more precise,…

  • Capturing Groups

    Now we are going to cover another useful feature of JavaScript regular expressions: capturing groups, allowing to capture parts of a string, putting them into an array. It has two primary effects: Allows getting a part of the match as a separate item in the result array. In case of putting a quantifier after the parentheses, it applies…

  • Greedy and Lazy Quantifiers

    There are two operation modes for quantifiers in JavaScript. In this chapter, we will see how the search works with greedy and lazy quantifiers. Imagine you have a text and need to replace all the quotes “…” with guillemet marks «…». For instance, “Welcome to Docs”, must become «Welcome to Docs». Here, the first step should be locating the quoted strings and then replacing them. At…

  • Quantifiers +, *, ? and {n}\

    In JavaScript, quantifiers are used for specifying the numbers of characters or expressions to match. To a better understanding, imagine having a string like +3-404-777-41-32 and want to find all the full numbers in it: 3, 404, 777, 41, 32. A number is known as a sequence of one or more digits \d. Quantifiers are used for marking how many of…

  • JavaScript Sets and ranges

    Let’s get deeper into the details of regular expressions. In this chapter, we will show you how to use sets and ranges in JavaScript. Putting several characters or character classes inside square brackets allows searching for any character among the given. To be precise, let’s consider an example. Here, [lam] means any of the given three characters ‘l’, ‘a’,…