当前位置:网站首页>Typescript regular expression use
Typescript regular expression use
2022-07-20 22:49:00 【RemoteDev】
console.log('ts debug');
let regex = new RegExp('ttt','i'); //i Modifier It's not size sensitive
console.log(regex.test('test/ttt/a'));
console.log(new RegExp(/abc/ig,'i').flags);
// Regular u Modifier usage
// The treatment is greater than \uFFFF Of Unicode character
console.log(/^\uD83D/u.test('\uD83D\uDC2A'));//false // Correctly identify
console.log(/^\uD83D/.test('\uD83D\uDC2A'));//true No addition u Modifier Error identification , Only recognize the previous character
//. Modifier
console.log(/^.$/.test('𠮷'));//false No addition u Modifications are considered 2 Characters . Match all single characters Except for special characters \n \r Row separation Segment separation (U+000A U+000D U+2028 U+2029)
console.log(/^.$/u.test('𠮷'));//true Add u Modifier Correctly identify
//Unicode Character test
console.log(/\u{61}/.test('a'));
console.log(/\u{61}/u.test('a'));// Add u Correctly identify unicode character \u{61}
console.log(/\u{20BB7}/u.test('𠮷'));// Add u Correctly identify unicode character \u{20BB7}
console.log(' Quantifier recognition ', /K{5}/.test('KKKKK')); // Quantifier recognition
console.log( ' Quantifier recognition ',/K{5}/u.test('KKKKK')); // Quantifier recognition
console.log(' Quantifier recognition ', /𠮷{5}/.test('𠮷𠮷𠮷𠮷𠮷')); // Quantifier recognition
console.log( ' Quantifier recognition ',/𠮷{5}/u.test('𠮷𠮷𠮷𠮷𠮷')); // Quantifier recognition
console.log(/^\S$/.test('𠮷'));//\S Match all non whitespace characters /^ Match the beginning of the line $/ Match the end of the line
console.log(/^\S$/u.test('𠮷'));//\S Match all non whitespace characters
// Use . and s Match all characters, including special characters
console.log(/aaa.bbb/s.test('aaa\nbbb'));//true Match correctly to \n Enter special character
let tmpReg = new RegExp('OOO.KKK','s');
console.log(' Recognize carriage return characters :',tmpReg.test('OOO\nKKK'),'dotAll Pattern :',tmpReg.dotAll,' Use modifier :',tmpReg.flags);
let getCodePointLength=(text)=>{
let r = text.match(/[\s\S]/gu); //\S Filter out spaces \s Match any whitespace /g The global matching /u matching Unicode character
return r ? r.length : 0 ;
}
console.log( '𠮷𠮷𠮷'.length,getCodePointLength('𠮷𠮷𠮷'));
console.log(/[a-z]/i.test('\u212A'));
console.log(/[a-z]/iu.test('\u212A'));// Add u Modifier can match Unicode character
console.log(' matching 0-9 Numbers :',/^\d/.test('123456'));
// Adhesion regularity test (sticky)
let str = 'kkk_kk_k';
let p1 = /k+/g;// The global matching Many times ok character There is no location requirement
let p2 = /k+/y;// The global matching y Modifier requirements must match from the beginning
console.log(p1.exec(str),p2.exec(str));
console.log(p1.exec(str),p2.exec(str));
let str2 = 'kkk_kk_k';
let p3 = /k+/g;// The global matching Many times ok character There is no location requirement
let p4 = /k+_/y;// The global matching y Modifier requirements must match from the beginning
console.log(p3.exec(str2),p4.exec(str2));
console.log(p3.exec(str2),p4.exec(str2));
let _REGEX = /a/g;// The global matching a character
_REGEX.lastIndex = 2;// From the first... Of the string 2 Two characters to start matching
const m = _REGEX.exec('baba');
console.log(' Find the matching location :',m.index);
console.log(_REGEX.exec('baba'));//null , Because the last search location was 3, This time from the position 4 Begin your search , Location 4 No characters
let _REGEX2 = /a/y;// The global matching a character , Match from the start
_REGEX2.lastIndex = 2;// From the first... Of the string 2 Two characters to start matching
const m1 = _REGEX2.exec('baba');//null ba Non cohesive If it is 'baaa' Continuous adhesion Just OK
if(m1){
console.log(' Find the matching location :',m1.index,m1);
}else{
console.log(' No matching location found :',m1);
}
let _REGEX3 = /a/y;// The global matching a character , Match from the start
_REGEX3.lastIndex = 3;// From the first... Of the string 2 Two characters to start matching
const m2 = _REGEX3.exec('baba');//null
if(m2){
console.log(' Find the matching location :',m2.index);
console.log(m2);
}
let xa = '@@@abc'.split(/@/y);// Will replace @ Empty string into array , because y The modifier specifies that the beginning matches
console.log(xa);//['','','','abc'];
console.log('a#b#c'.split(/#/y));//['a','b','c']
console.log('[email protected]@c'.split(/@/y));//['a','b','c']
console.log('[email protected]@@c'.split(/@/y));//['a','b','c'] //y Identify adhesive , Generate empty elements ''
console.log('a##b#c'.split(/#/y));//['a','b','c']
console.log(new RegExp(/[0-9]*/,'s').test(''));// Add * At least 0 Numbers or infinite numbers , No addition * At least one number
console.log(new RegExp(/[0-9]/,'s').test('1'));// Add * At least 0 Numbers or infinite numbers , No addition * At least one number
console.log(new RegExp(/[0-9]{8,16}/,'s').test('12345678'));//{8,16} least 8 A digital , most 16 A digital
console.log("======",new RegExp(/[0-9]{8,16}/,'s').test('1234a5678'));//{8,16} least 8 A digital , most 16 A digital
console.log(new RegExp(/[0-9]{8,16}/,'s').test('1234567887654321'));//{8,16} least 8 A digital , most 16 A digital
console.log(new RegExp(/[a-z]*/,'s').test(''));// Add * At least 0 Lowercase letters or unlimited letters , No addition * At least one letter
console.log(new RegExp(/[a-z]/,'s').test('a'));// Add * At least 0 Lowercase letters or unlimited letters , No addition * At least one letter
console.log(new RegExp(/[A-Z]*/,'s').test(''));// Add * At least 0 Lowercase letters or unlimited letters , No addition * At least one letter
console.log(new RegExp(/[A-Z]/,'s').test('A'));// Add * At least 0 Lowercase letters or unlimited letters , No addition * At least one letter
console.log(new RegExp(/[a-z]{8,16}/,'s').test('abcdefgh'));//{8,16} least 8 Lowercase letters , most 16 Lowercase letters
console.log(">>>>>>",new RegExp(/[a-z]{8,16}/,'s').test('abcdef123ghijop'));//{8,16} least 8 Lowercase letters , most 16 Lowercase letters
console.log(new RegExp(/[A-Z]{8,16}/,'s').test('AABBCCDD'));//{8,16} least 8 Capital letters , most 16 Capital letters
console.log(new RegExp(/[A-Z]{8,16}/,'s').test('AABBCCDDAABBCCDDDD'));//{8,16} least 8 Capital letters , most 16 Capital letters
console.log('ID REGEX:',new RegExp(/[0-9]{6}-\d{8}-\d{4}/,'s').test('000000-00000000-0000'));
console.log('ID REGEX:',new RegExp(/199528-\d{8}-\d{4}/,'s').test('199528-00000000-0000'));
console.log(new RegExp(/[\da-zA-Z]/,'y').test('*$%'));// At least alphanumeric
console.log(new RegExp(/[\da-zA-Z][^*$%#@]/,'y').test('Za'));// One that contains at least alphanumeric , Can not contain *$%#@ These special characters At least two characters
console.log('>>>>>>>',new RegExp(/[0-9][a-z][A-Z]/).test('1qS'));// At least there are numbers , One upper and lower case character
console.log(' Special character test :',new RegExp(/[^@#$%^&*~!<>=?]/).test('$%^')); // It cannot contain ^@#$%^&*~!<>=? These characters
// Regular global rules
let testreg = 'aaabbbcccdddeeefffgggg';
let rep = testreg.replace(/d/ig,'*');// Replace from anywhere
console.log(rep);
console.log(new RegExp(/d/y,'i').exec(testreg));
console.log(testreg.replace(/a/gy,'%'));// Replace from the beginning
console.log('n1n2n3n4n5n6'.match(/n\d/y));// Match only the first //n1 Match the letter + Number combination n It's followed by numbers : nx x-> Numbers
console.log('n1n2n3n4n5n6'.match(/n\d/gy));// add g Match all //n1 n2 n3 n4 n5 n6 Match the letter + Number combination n It's followed by numbers : nx x-> Numbers
const _reg_ex_num = /\d(\+)\d/y;
console.log(_reg_ex_num.test('1+2'));
边栏推荐
- Matrikon OPC 模拟器使用教程
- Microorganisms are closely related to our life
- C language file operation
- 【论文阅读|浅读】RolX: Structural Role Extraction & Mining in Large Graphs
- Typescript正则表达式使用
- The introduction of 23 Filipino doctors for 18million was a hot topic, and the school teacher responded: expedient
- 7-15 operation
- 1054 The Dominant Color
- 什么是Nacos及实战使用教程
- m基于matlab的IEEE802.15.4家庭网络高效节能的有效接入方法
猜你喜欢
“先导杯”来啦!召唤科学计算界的最强大脑,36万奖池等你来拿!
mysql建库建表(一)
2022年北京产品经理认证招生简章(NPDP)
leetcode:952. 按公因数计算最大组件大小
TikTok直播带货助力产业出海,FastData观察行业精品沙龙助力生态发展
从wolai转移到Notion
leetcode:460. LFU least commonly used cache
UniPro多端部署 满足客户多样化需求
CPDA|先学数据分析工具还是数据分析思维?
[paper reading | deep reading] verse: versatile graph embeddings from similarity measures
随机推荐
D static import object
1055 The World‘s Richest
TikTok直播带货助力产业出海,FastData观察行业精品沙龙助力生态发展
MySQL addition, deletion, query and modification
After Flink's sink table was inserted into the MySQL table, the time was reduced by 8 hours. Who knows what the problem is?
掌动智能国产性能测试工具XRunner正式亮相QECon
软件测试技术之项目上线流程
简单理解 V8 Turbofan
Matlab 语法小问题 【问答随笔·4】
TIA博途_STEP7版本的升级与移植相关难点汇总
CPDA | first learn data analysis tools or data analysis thinking?
Yiyang Qianxi was scolded for being hot search...
TIA botu_ Summary of difficulties related to the upgrade and migration of STEP7 version
visual studio 2019 安装卸载问题
XML to VOC, VOC to coco, coco to Yolo, coco partition, coco check, Yolo check, coco visualization
m基于光纤光栅传感网接入GPON的光纤通信系统matlab性能仿真,包括解码,解封装,分接,码率恢复,解帧,拆包,译码
The eighth anniversary of the founding of crowdsource bit and the first anniversary of crowdsource chain officially released the New World Declaration
EMQ映云科技成功入选《中国企业家》2022年度「新锐100」榜单
Apple and other big factories can't hold anymore! Layoffs and slowing recruitment, "two pronged" plan
华为机试:找城市