Discuss in detail the impact of lastIndex on regular results
When using regular expressions to check the same string, true and false are returned alternately. In desperation, I looked through the authoritative guide again and found that the culprit turned out to be lastIndex. This article will share with you the impact of lastIndex on regular results.
let reg = /[\d]/g //undefined reg.test(1) //true reg.test(1) //false
lastIndex
lastIndex is explained as follows in The Definitive Guide: It is a readable/ written integer. If the matching pattern has the g modifier, this attribute is stored at the beginning of the next index in the entire string. This attribute will be used by exec() and test(). Still using the above example, observe the lastIndex attribute
##
let reg = /[\d]/g //有修饰符g //undefined reg.lastIndex //0 reg.test(1) //true reg.lastIndex //匹配一次后,lastIndex改变 //1 reg.test(1) //从index 1 开始匹配 //false reg.lastIndex //0 reg.test(1) //true reg.lastIndex //1
Solution
1. Do not use the g modifier
reg = /[\d]/ ///[\d]/ reg.test(1) //true reg.test(1) //true reg.lastIndex //0 reg.test(1) //true reg.lastIndex
2. Manually set lastIndex = 0 after test()
The above content is the lastIndex pair The impact of regular results, I hope it can help everyone. Related recommendations:Detailed explanation of the use of lastIndexOf() method for arrays and strings in JavaScript_Basic knowledge
Usage of indexOf and lastIndexOf Example introduction_javascript skills
js lastIndexOf() usage example
The above is the detailed content of Discuss in detail the impact of lastIndex on regular results. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What happens when someone calls in airplane mode? Mobile phones have become one of the indispensable tools in people's lives. It is not only a communication tool, but also a collection of entertainment, learning, work and other functions. With the continuous upgrading and improvement of mobile phone functions, people are becoming more and more dependent on mobile phones. With the advent of airplane mode, people can use their phones more conveniently during flights. However, some people are worried about what impact other people's calls in airplane mode will have on the mobile phone or the user? This article will analyze and discuss from several aspects. first

When trying to open a disk image in VirtualBox, you may encounter an error indicating that the hard drive cannot be registered. This usually happens when the VM disk image file you are trying to open has the same UUID as another virtual disk image file. In this case, VirtualBox displays error code VBOX_E_OBJECT_NOT_FOUND(0x80bb0001). If you encounter this error, don’t worry, there are some solutions you can try. First, you can try using VirtualBox's command line tools to change the UUID of the disk image file, which will avoid conflicts. You can run the command `VBoxManageinternal

On the Douyin platform, users can not only share their life moments, but also interact with other users. Sometimes the comment function may cause some unpleasant experiences, such as online violence, malicious comments, etc. So, how to turn off the comment function of TikTok? 1. How to turn off the comment function of Douyin? 1. Log in to Douyin APP and enter your personal homepage. 2. Click "I" in the lower right corner to enter the settings menu. 3. In the settings menu, find "Privacy Settings". 4. Click "Privacy Settings" to enter the privacy settings interface. 5. In the privacy settings interface, find "Comment Settings". 6. Click "Comment Settings" to enter the comment setting interface. 7. In the comment settings interface, find the "Close Comments" option. 8. Click the "Close Comments" option to confirm closing comments.

Java is a commonly used programming language used to develop various applications. However, just like other programming languages, Java has security vulnerabilities and risks. One of the common vulnerabilities is the file inclusion vulnerability (FileInclusionVulnerability). This article will explore the principle, impact and how to prevent this vulnerability. File inclusion vulnerabilities refer to the dynamic introduction or inclusion of other files in the program, but the introduced files are not fully verified and protected, thus

The impact of data scarcity on model training requires specific code examples. In the fields of machine learning and artificial intelligence, data is one of the core elements for training models. However, a problem we often face in reality is data scarcity. Data scarcity refers to the insufficient amount of training data or the lack of annotated data. In this case, it will have a certain impact on model training. The problem of data scarcity is mainly reflected in the following aspects: Overfitting: When the amount of training data is insufficient, the model is prone to overfitting. Overfitting refers to the model over-adapting to the training data.

Bad sectors on a hard disk refer to a physical failure of the hard disk, that is, the storage unit on the hard disk cannot read or write data normally. The impact of bad sectors on the hard drive is very significant, and it may lead to data loss, system crash, and reduced hard drive performance. This article will introduce in detail the impact of hard drive bad sectors and related solutions. First, bad sectors on the hard drive may lead to data loss. When a sector in a hard disk has bad sectors, the data on that sector cannot be read, causing the file to become corrupted or inaccessible. This situation is especially serious if important files are stored in the sector where the bad sectors are located.

Some users may consider buying mining cards for the sake of cheapness. After all, these cards are top-notch graphics cards. However, some gamers are worried about the impact of mining cards on playing games. Let’s take a look at the detailed introduction below. What are the effects of using a mining card to play games: 1. The stability of playing games with a mining card cannot be guaranteed, because the life of the mining card is very short and it is likely to become useless after just playing. 2. The mining card is basically a castrated version of the original version. Due to long-term wear and tear, the performance in all aspects may be weak. 3. In this way, users may not be able to display all the effects of the game when playing the game. 4. Moreover, the electronic components of the graphics card will age in advance, not to mention that playing games also consumes the graphics card, so it is drained to a greater extent, so the impact on the game is great. 5. In general, use mining cards to play games

The running quality of a computer basically has a huge impact on its graphics card. Some users don’t know much about graphics cards, and they don’t know exactly what aspects of the computer the graphics card will affect. For your convenience, here is what you need to know: Let’s introduce some effects of low graphics card configuration. What are the effects of low graphics card configuration? Answer: 1. Some large-scale 3D games cannot run. 2. When playing some high-definition videos, the computer will be under great pressure. 3. For some more professional software, there is no way to run it well when drawing and 3D model rendering are required. 4. If the graphics card configuration is low, the game will not be able to open, or it will frequently crash or freeze, and the computer will also have a blurred screen or a blue screen. 5. The most important thing in the game is the graphics card, because many pictures require
