当前位置:网站首页>JS regular advanced code to understand
JS regular advanced code to understand
2022-07-18 13:10:00 【Light of D】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>1</p><p>2</p>
<script> // Laziness matches let text = '<p>1</p><p>2</p>'; let pattern0 = /<p>.*<\/p>/; let pattern = /<p>.*?<\/p>/; console.log(' Greedy matching ',pattern0.exec(text)); console.log(' Laziness matches ',pattern.exec(text)); // grouping () text = "mom and dad and baby"; pattern = /mom( and dad( and baby))/; console.log(' grouping ',pattern.exec(text)); // Non capture grouping (?:) pattern = /mom(?: and dad)( and baby)/; console.log(' Non capture grouping ',pattern.exec(text)); // Backtracking reference of group text = '<p>1</p><p>2</p>'; pattern0 = /<(p)>.*<(\/\1)>.*<\1>.*<\2>/; console.log(' Backtracking reference of group ',pattern0.exec(text)); // Named capture groups (?<name> Content ) ES9 New characteristics // How to access the exec The returned result is named groups Properties of , This object has the attribute we named text = "mom and dad and baby"; pattern = /mom(?<dad_baby> and dad(?<baby> and baby))/; let result = pattern.exec(text); console.log(' Named capture groups ',pattern.exec(text)); console.log(result.groups.dad_baby); console.log(result.groups.baby); // Forward assertion (?= expression ) To look right in a position , Indicates that the right side of the location must match the expression text =' I like you I like I like me like Like you, '; pattern = / like (?= you )/g; var a; console.log(' Forward assertion ', a=pattern.exec(text),'index',a?.index); console.log(' Forward assertion ',a=pattern.exec(text),'index',a?.index); console.log(' Forward assertion ',a=pattern.exec(text),'index',a?.index); // Reverse look ahead assertion (?! expression ) pattern = / like (?! you )/g; console.log(' Reverse look ahead assertion ', a=pattern.exec(text),'index',a?.index); console.log(' Reverse look ahead assertion ',a=pattern.exec(text),'index',a?.index); console.log(' Reverse look ahead assertion ',a=pattern.exec(text),'index',a?.index); console.log(' Reverse look ahead assertion ',a=pattern.exec(text),'index',a?.index); // Forward and backward assertions :(?<= expression ), To look left in a position , Indicates that the left side of the location must match the expression pattern = /(?<= I ) like (?= you )/g; console.log(' Forward and backward assertions ', a=pattern.exec(text),'index',a?.index); console.log(' Forward and backward assertions ',a=pattern.exec(text),'index',a?.index); // Reverse backward assertion :(?<! expression ), To look left in a position , Indicates that the left side of the location cannot match the expression pattern = /(?<! I ) like (?! you )/g; console.log(' Reverse backward assertion ', a=pattern.exec(text),'index',a?.index); console.log(' Reverse backward assertion ',a=pattern.exec(text),'index',a?.index); // Regular extension :dotAll Pattern ,s Represents this pattern //dot . Metacharacters Any single character other than the newline character let str = ` <ul> <li> <a> Shawshank's salvation </a> <p> Release date : 1994-09-10</p> </li> <li> <a> Forrest gump </a> <p> Release date : 1994-07-06</p> </li> </ul>`; // Declare regular // const reg = /<li>\s+<a>(.*?)<\/a>\s+<p>(.*?)<\/p>/; const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/gs; // Perform matching // const result = reg.exec(str); let result0; let data = []; while (result0 = reg.exec(str)) {
data.push({
title: result0[1], time: result0[2] }); } // Output results console.log(data); // Assert advanced applications // Assertions can be used to determine whether a string conforms to specific rules , For example, extract at least 8 A string pattern = /(?=.*?[a-z])(?=.*?[A-Z]).{8,}/gm; pattern0 = /.{8,}(?<=[a-z].*?)(?<=[A-Z].*?)/gm; text =` Admin123456 pZUJLUpTL2 Tnut2eWPN1 wJxpVhVYi3 UySRo49ps Ig7AHzZ0J oYHMDdHCK9 yiyWKQnWo2 gTZEEkVrj1 8Ij12340as wdfqe#wefDdf444 Codejiaonang123 [email protected] 111111abc11ABc CodeJiaonang123 qwe 8848 123456 asd123 Adm123 Asd123 wjleif932 admin123 123admin 123asd123 ADMIN123() Number 89757 888888888info masterxiao123 888888888A ` for (let i =0 ;i<16;i++){
a=pattern.exec(text); console.log(' Standardize password mode 1 ', a?a[0]:' period '); } for (let i =0 ;i<16;i++){
a=pattern0.exec(text); console.log(' Standardize password mode 2 ', a?a[0]:' period '); } </script>
</body>
</html>
边栏推荐
- 还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS ~
- Rust webserver service
- 【性能测试】性能测试问答篇
- Where is the motivation to be a test / development programmer? How to persist
- Miller_ Rabin Brief
- JS small effect jump according to the corresponding item name
- hcip静态综合复习实验
- 广电5G正式进场:没有四国混战,而是两线对抗
- 【编程强训10】井字棋+密码强度等级
- [golang | GRC] GRC server streaming service end stream practice
猜你喜欢

hcip第五天筆記

Hcip day 5 notes

【剑指 Offer】从尾到头打印链表(栈思想)+ 数组中重复的数字(哈希映射)

为什么 UDP 头只有 8 个字节

【Golang | gRPC】使用TLS/SSL认证的gRPC服务

Grid layout

ES6 browser support and running environment support detection and ES6 transcoding Es5
![[full text 7000 words] SF city test development side 49min answer](/img/21/87dfa2c999eda48c89951d5a6af8b0.png)
[full text 7000 words] SF city test development side 49min answer

What is the future of privacy computing and NFT under the wave of Web3.0

Harbor: modify the default 172 network segment
随机推荐
STL小知识点
自动追番工具BGmi
Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two
hcip第六天笔记
This week's investment and finance report: many venture capital institutions have completed fund-raising, and Web3.0 track continues to attract money
[golang | GRC] GRC server streaming service end stream practice
金仓数据库 KingbaseES SQL 语言参考手册 (3.1.1.4. 日期/时间类型)
内存管理页面属性
Object conversion problems
ReversingKr-wp(7)
东方甄选爆火背后的流量密码
报错解决——RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton
ES6 browser support and running environment support detection and ES6 transcoding Es5
【Swoole系列2.4】WebSocket服务
Analysis of Wireshark Ethernet frame
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
zip/enumerate/map函数的使用
Rust iter& match
Comment systématiser gratuitement l'introduction à la science des données?
Hyperspace travel solution