Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report