Category: 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 you want to get the result as an array, you should write it as follows: str.matchAll(regexp)…

  • 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…

  • 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<name>

    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: Examples of Using Parentheses Now, let’s see how parentheses operate. Imagine, you have an example “dododo”. Without using parentheses, the pattern do+ means d character, followed by o and repeated one or more…

  • 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 W3Docs”, must become «Welcome to W3Docs». Here, the first step should be locating the quoted strings and then replacing them. At…

  • 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’,…

  • Escaping, Special Characters

    The escape function is a global object property. It figures a new string where particular characters are replaced by a hexadecimal escape sequence. The special characters are usually encoded by using the exception of @*_+-./. They are used mainly for doing more powerful searches. The full list of the special characters looks as follows: [ \ ^ $ . | ?…

  • Word Boundary: \b

    The word boundary \b corresponds to positions when one of the sides is a word character, and the other one is not. Word boundaries are especially handy when it is necessary to match a sequence of letters or digits on their own. Or it is useful when you want to ensure that they happen at the start…