Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the compile method of RegExp object in js regular expression

Shulou Source: shulou.com Published: 2022-06-02 03:45:59 10月01日 Update

Editor to share with you how to use the compile method of the RegExp object in js regular expressions, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

Function description: this method can compile the specified regular expression, the regular expression execution speed will be improved after compilation, if the regular expression is called many times, then calling the compile method can effectively improve the execution speed of the code, if the regular expression can only be used once, there will be no obvious effect.

Basic syntax: objReg.compile (pattern [, flag])

ObjReg required, the name of the RegExp object variable

Pattern required regular expression

Flag optional matching option

The code is as follows:

/ / string to match

Var objStr= "my mobile number is 13522222222, his mobile number is 13233333333, her mobile number is 13988888888"

/ / set the regular expression to match 11 starting with 13 as the mobile phone number, and global matching (in this case, the matching mobile phone number)

Var reg=new RegExp ("13 [4-9] (/ / d) {8}", "g")

/ / prompt the user to find the mobile number, and then print the result

[xss_clean] ("Mobile phone number found")

/ / call the function to print the matching result

FindPhoneNumbers (objStr.match (reg))

/ / recompile regular expressions

Reg.compile ("13 [0-3] (/ / d) {8}", "g")

[xss_clean] ("Discovery Unicom Mobile number")

FindPhoneNumbers (objStr.match (reg))

/ / define the output return result function

Function findPhoneNumbers (arr) {

/ / use loops to output data

For (var iTuno Bandi)

Tags: Regular expression mobile phone number method mobile number compilation object article result mobile code content function mobile number speed output obvious effective not so much Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Shulou Tech Info MySQL Linux