js regular expression lookahead and lookbehind and non-capturing grouping

js regular expression lookahead and lookbehind and non-capturing grouping

Combining lookahead and lookbehind with capture groups

In real application scenarios, capturing groups or non-capturing groups are usually limited to lookahead and lookbehind conditions. For example, when formatting the number 12345678, the result is 12,345,678. Its regular implementation is as follows:

let formatSum = '12345678'.replace(/\B(?=(?:\d{3})+(?!\d))/g, ',')

Capturing and non-capturing groups

In order to understand lookahead and lookbehind, we must first understand capturing groups and non-capturing groups

In js,

() means capturing groups, () will save the matching values ​​in each group, using $n (n is a number, indicating the content of the nth capturing group);

(?:) represents a non-capturing group. The only difference from a capturing group is that the value matched by the non-capturing group is not saved.

Taking the formatSum expression as an example, (?=(?:\d{3})+(?!\d)), (?:\d{3}), (?!\d) are all groups, and the second group is a non-capturing group.

Looking forward, looking back, and looking forward and looking back negatively

In the above formatSum expression, '?=' and '?!' are used, which are the so-called lookahead and negative lookahead. To make it easier to understand, let's start with a simple example.

// Lookahead:
A(?=B) //Find A before B
// Lookback:
(?<=B)A //Find A after B
// Negative lookahead:
A(?!B) //Find A that is not followed by B
// Negative lookback:
(?<!B)A //Find A that is not preceded by B

Looking back at the formatSum expression, (?:\d{3})+(?!\d) is considered as an overall expression A, that is,

formatSum = /\B(?=A)/g //Here A is an expression, not the actual letter A, just for ease of understanding

It means to match the \B in front of expression A, and \B matches non-letter boundaries, so it can be seen that the overall function of the expression is to match and replace the boundary in front of expression A.

The counterpart to \B is \b, which matches a letter boundary. For beginners, the concept of boundary is difficult to understand. You can think of it as an invisible |. There is a boundary in any string with a length greater than or equal to 2. For example, 'ab' can be seen as 'a|b', except that the | is invisible and is not counted in the string length.

'ab'.replace(/\B/, ',')
// a,b

Next let's look at part A of the expression: (?:\d{3})+(?!\d) .

First, ?: represents a non-capturing group, \d{3} represents 3 digits, and (?:\d{3})+ represents 3, 6, 9, 12... digits;

(?!\d) is a negative lookahead, which means to match (?:\d{3})+ that is not followed by a number. In summary:

(?:\d{3})+(?!\d)  

Matches 3*n (n=1 increments) digits in '12345678' that are not numbers, i.e. '678', '345678'

So, the result is:

formatSum = '12345678'.replace(/\B(?=(?:\d{3})+(?!\d))/g, ',')

Matches the non-letter boundary before the 3*n (n=1, n++) digits in '12345678' that are not numbers.
That is, the non-letter boundaries before '678' and '345678' are finally replaced with commas.
That is, add a comma before '3' and '6'

Right now

'12345678'.replace(/\B(?=(?:\d{3})+(?!\d))/g, ',') === '12,345,678'
// true

Summarize

This is the end of this article about the lookahead, lookbehind and non-capturing groups in js regular expressions. For more information about lookahead, lookbehind and non-capturing groups in js regular expressions, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the practical application of regular expressions in JavaScript
  • js uses regular expressions to filter year, month and day examples
  • js regular expression simple verification method
  • Js uses regular expressions to remove brackets from strings
  • JavaScript uses regular expressions to implement registration and login verification
  • JavaScript Regular Expressions Explained

<<:  Docker starts Redis and sets the password

>>:  Detailed tutorial on installing and configuring MySql5.7 on Ubuntu 20.04

Recommend

How to write configuration files and use MyBatis simply

How to write configuration files and use MyBatis ...

Summary of Git commit log modification methods

Case 1: Last submission and no push Execute the f...

Example of exporting and importing Docker containers

Table of contents Exporting Docker containers Imp...

Seven solutions for classic distributed transactions between MySQL and Golan

Table of contents 1. Basic theory 1.1 Transaction...

Solve the mobile terminal jump problem (CSS transition, target pseudo-class)

Preface Many friends who have just come into cont...

A brief talk about MySQL pivot tables

I have a product parts table like this: part part...

How to implement page jump in Vue project

Table of contents 1. Create a vue-cli default pro...

A brief talk about Rx responsive programming

Table of contents 1. Observable 2. Higher-order f...

Implementation of k8s node rejoining the master cluster

1. Delete node Execute kubectl delete node node01...

JavaScript implements countdown on front-end web page

Use native JavaScript to simply implement the cou...

Using vsftp to build an FTP server under Linux (with parameter description)

introduce This chapter mainly introduces the proc...

Summary of Mysql-connector-java driver version issues

Mysql-connector-java driver version problem Since...

Implementing custom scroll bar with native js

This article example shares the specific code of ...