The difference between the atom group and the atom table is that the atom group matches multiple elements at a time, while the atom table matches any character
1. When the g pattern modifier is not added, only the first one is matched, and the matched information contains the following data 2. Using atomic group matching in the match, each group of data will be returned to the result
// 全局匹配<body><main><h1>内容1部分</h1><h2>内容1部分</h2><h3>内容1部分</h3></main></body><script>let val =/<(h[1-6])>([\s\S]+?)<\/\1>/gi;let body = document.body;let hd = body.innerHTML.matchAll(val);
console.log(hd);let arr =[];for(const iterator of hd){
arr.push(iterator[2]);}
console.log(arr);</script>
// 兼容方法
String.prototype.matchAll=function(reg){
let res =this.match(reg);if(res){
let str =this.replace(res[0],"^".repeat(res[0].length));let match = str.matchAll(reg)||[];return[res,...match];}};
// 正则的方法匹配functionsearch(param, reg){
let result =[];while((ee = reg.exec(param))){
result.push(ee);}return result;}let matchs =search(document.body.innerHTML,/<(h[1-6])>[\s\S]+?<\/\1>/gi);
console.log(matchs);
Insert the content to the left of the currently matched substring.
$’
Insert the content to the right of the currently matched substring.
$n
If the first parameter is a RegExp object, and n is a non-negative integer less than 100, then insert the string matching the nth bracket. Tip: Index starts from 1
// 正则方法// test 检测输入的邮箱是否合法<body><input type="text" name="email"/></body><script>let email = document.querySelector(`[name="email"]`);
email.addEventListener("keyup", e =>{
console.log(/^\w+@\w+\.\w+$/.test(e.target.value));});</script>// exec 不使用 g 修饰符时与 match 方法使用相似,使用 g 修饰符后可以循环调用直到全部匹配完<body><div class="content">
后盾人不断分享视频教程,后盾人网址是 houdunren.com
</div></body><script>let content = document.querySelector(".content");let reg =/(?<tag>后盾)人/g;let num =0;while((result = reg.exec(content.innerHTML))){
num++;}
console.log(`后盾人共出现${
num}次`);</script>