Advanced regular examples

How to learn is regular, it has been my distressing thing, sometimes I write regular, himself does not understand why, try holding the attitude

But I really want to learn, here I will come out with an example, for every regular write his understanding (Fundamentals I do not write, try to write some of the little details)

I will look at the reference section <<regular guidelines>>

Multiple choice

Vertical | separate alternative, but | particularly low priority, you can use parentheses wrapped him up

Complicated | write simple

Designed specifically for capturing group exec

Super strong, but I found out today (2019/05/10)

Default matching entry is returned, through the additional matching content packet, obtained by []

let str = "aaabbb";
/*可以匹配到分组的内容*/
let reg=/(a+)(?:b+)/g;
let res = reg.exec(str);
console.log(res[1]);

let str = "cccddd";
/*可以匹配到分组的内容*/
let reg=/(c+)(d+)/g;
let res = reg.exec(str);
console.log(res[1]);

Good introductory information link

replace

第二个参数是函数
let url = "https://www.baidu.com?a=1&b=2";
let reg=/([^&#?+]+)=([^&?#+]+)/g;
let obj = {};
//$0  返回整体  $1返回第一个分组  $2 返回第二个分组
url.replace(reg, ($0,$1, $2) => obj[$1] = $2);
console.log(obj);

Backreferences

\1 到\10
console.log('aaa bbbb ffffff 999999999'.match(/(\w)(?=\1\1\1)(\1)+/g));

反向引用的作用通常是用来查找或限定重复、查找或限定指定标识配对
([a-z])\1{2}
忽略分组  ?:
正向断言  ?=
反向断言  ?!
    test  判断return false
    exec  查看分组
    search  返回第一个匹配的索引 到不到 -1
    match  返回数组
    split
    replace
//找到重复的单词
const str = 'This is the theater you you have been to to';
str.replace(/\b([a-z]+)\s\1\b/ig,(match,...args)=>{
  console.log(args[0]);
})

Looking around (and more to find information)

环视,在不同的地方又称之为零宽断言,简称断言。
?=P   可定顺序
?!P   否定顺序
?<=P  肯定逆序
?<!P  否定逆序

console.log(/(http|ftp|svn|abc)ddd/g.test('httpddd'));

 Constant 锚点

.*?  如果换行失败 改成 [\s\S]*?  

Positive integer + decimal

console.log(/^[+]?[1-9]\d*(?:\.\d)?$/.test('+2.1'));

Looking around

?=PAffirmative ?!Pnegative

(?=A)[A-Z]

(? = A) location, followed by the A

[AZ] matches any one of the letters AZ

Combined

From left to right are regular match looked around (?=A)and [A-Z], looking around because the match does not consume regular character, but also because of match A

 let s1='abcd abcd abbdABCD';
    console.log(s1.match(/(?=a)[a-z]/g)); // ["a","a","a"]
字母序列后面跟着;
    let s1='aaaa;bbb;cccc;dddd;';
    console.log(s1.match(/[a-z]+(?=;)/g)); //["aaaa", "bbb", "cccc", "dddd"]
常规匹配   
    let s1 = 'notexefile1.exe';
    console.log(/.+(?=\.exe)/g.exec(s1)); //notexefile1

Variant usage

肯定变种

需求包含字母(不区分大小写),数字,6-16为密码
^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9]{6,16}$
  讲解
  使用(?=.*?[a-zA-Z])限定后面的字符中至少有一个字母
    使用(?=.*?[0-9])限定后面的字符中至少有一个数字
    最后通过实际匹配正则[a-zA-Z0-9]{6,16}限定量词
    
否定变种

获取不是.exe后缀文件不含后缀的文件名 
正则:(.+)(?!\.exe)\.[^.]+$ 

肯定逆序 (?<=P)

js不支持,但是好像是用webpack的时候是支持的
需求:获取name参数的值 
正则:(?<=name=).+
示例很直白,前面必须是name=,然后获取其后面的数据  
      let s1 = 'name=Zjmainstay';
    console.log(/(?<=name=).+/g.exec(s1));// Zjmainstay

Regular Block combination method - must contain letters, numbers, special characters

Regular:^(?=.*?[a-z])(?=.*?\d)(?![a-z\d]+$).+$

Analysis:  (?=.*?[a-z])restrictions must have a letter

(?=.*?\d)Restrictions must be digital

(?![a-z\d]+$)Limit from the beginning to the end can not be all numbers and letters

.+In the absence of any character can be defined  ^and the $ beginning and end of the string defined

Regular gradual improvement of law - exclude particular tag p/a/img, matching html tags

Regular:</?(?!p|a|img)([^> /]+)[^>]*/?>

Starting from simple labels, for </p>and <br />, write regular:  </?[^>]*/?>

We have to be observed, the tag name is obtained in this way:

无属性值:<p>           <([^>]+)
有属性值:<p class      <([^ ]+)
无属性值自闭合:<br/>   <([^/]+)
闭合标签:</p>          </([^>]+)>

Get regular:

</?([^> /]+)

Finally, we need to exclude p/a/imglabels, looking around with a negative sequential method, adding negative in front of the tag name:  </?(?!p|a|img)([^> /]+)[^>]*/?>

正则减除查错法-匹配异常原因查找

<ul>
    <li class="item">item1</li>
    <li class="item">item2</li>
    <li class="item bug">item3</li>
    <li class="item">item4</li>
    <li class="item">item5</li>
</ul>

正则:<li class="item">(.*?)</li>

问题"item bug" 怎么解决

正则<li class="item[^"]*"

最终的正则<li class="item[^"]*">(.*?)</li>

正则练习题

https://www.zybuluo.com/Zjmainstay/note/709093

匹配个数为偶数且不能为空

/^(..)+$/

正则查找不包含某些字符串

^((?!不想包含的字符串).)*$

解释
(?!不想包含的字符串)   //是匹配位置
.是任意字符
表达式(?!hede).只执行一次
匹配0次或多次:((?!hede).)*

需求:匹配每行数据中以.jpg/.jpeg/.png/.gif结尾的图片名称(含后缀)

.+(?=\.(jpg|jpeg|png|gig)).+

......................................................................................................................................................................................................................................................................................................................................................................................................................

Guess you like

Origin www.cnblogs.com/fangdongdemao/p/10964189.html