31、为低端浏览器定义原型方法matchAll
<body>
<p>
<span>wujunjie.fun</span>
<span>junjie.com</span>
<span>wu.cn</span>
</p>
</body>
<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];
}
return null;
}
let hd = document.querySelector('p').innerHTML;
const resArr = hd.matchAll(/<span>([\s\S]+?)<\/span>/i);
console.log(resArr);
if(resArr.length) {
resArr.forEach(item => {
console.log(item[1]);
})
}
</script>
32、使用exec完成全局匹配
let str = document.querySelector('p').innerHTML;
let reg = /<span>([\s\S]+?)<\/span>/gi;
while(res = reg.exec(str)) {
console.log(res[1]);
}
33、search、match、matchAll、split、replace
<body>
<p>
<span>wujunjie.fun</span>
<span>junjie.com</span>
<span>wu.cn</span>
</p>
</body>
<script>
const html = document.querySelector('p').innerHTML;
let reg = /<span>([\s\S]+?)<\/span>/gi;
let str = 'wujunjie.fun';
console.log(str.search(/u/g));
console.log(html.search(reg));
console.log(str.match(/u/));
console.log(html.match(reg));
for (const iterator of html.matchAll(reg)) {
console.log(iterator);
}
let hd = '2020/03-23';
let hdReg = /[-\/]/;
console.log(hd.split(hdReg));
console.log(hd.replace(/[-\/]/g,'^'));
let wu = '(010)9999999 (020)8888888';
let wuReg = /\((\d{3,4})\)(\d{7,8})/g;
console.log(wu.replace(wuReg, '$1-$2'));
</script>
34、原子组别名优化正则
<body>
<p>
<span>wujunjie.fun</span>
<span>junjie.com</span>
<span>wu.cn</span>
</p>
</body>
<script>
const str = document.querySelector('p').innerHTML;
let reg = /<span>(?<con>[\s\S]+?)<\/span>/gi;
console.log(str.replace(reg,'<h1>$<con></h1>'));
</script>
35、test、exec
<body>
<p>
<span>wujunjie.fun</span>
<span>junjie.com</span>
<span>wu.cn</span>
</p>
</body>
<script>
const str = document.querySelector('p').innerHTML;
let reg = /<span>(?<con>[\s\S]+?)<\/span>/gi;
while(res = reg.exec(str)){
console.log(res);
}
</script>
36、断言匹配?=以及应用
<body>
<p><span>我叫吴俊杰,吴俊杰加油!!!</span></p>
</body>
<script>
let str = document.querySelector('p span');
let reg = /吴俊杰(?=加油)/g
str.innerHTML = str.innerHTML.replace(reg,`<a href='wujunjie.fun'>$&</a>`)
let newStr = `
js,200元,300次
php,300.00元,100次
node.js,180元,260次
`;
let newReg = /(\d+)(\.00)?(?=元)/gi;
newStr = newStr.replace(newReg,(v, ...args) => {
console.log(args);
args[1] = args[1] || '.00';
return args.splice(0,2).join('');
})
console.log(newStr);
</script>
37、断言匹配?<=以及应用
<body>
<p>
<span>我叫吴俊杰,吴俊杰加油!!!</span>
<a href="https://baidu.com"></a>
<a href="https://yahoo.com"></a>
</p>
</body>
<script>
let hd = 'wujunjie2019nihao170'
const reg = /(?<=wujunjie)\d+/gi;
console.log(hd.match(reg));
const main = document.querySelector('p');
const newReg = /(?<=href=(['"])).+(?=\1)/gi;
main.innerHTML = main.innerHTML.replace(newReg, 'https://www.wujunjie.fun');
let users = `
吴俊杰电话:18325516903
另一个人电话:00000000000
`;
let reg2 = /(?<=\d{7})\d{4}/gi;
console.log(users.replace(reg2, '****'));
</script>
38、断言匹配?!以及应用
<body>
<input type="text" name="username">
</body>
<script>
let str = 'wujunjie2019nihao';
let reg = /[a-z]+(?!0)/gi;
console.log(str.match(reg));
const input = document.querySelector(`[name='username']`);
input.addEventListener('keyup', function() {
const reg = /^(?!.*俊杰.*).*/gi;
console.log(this.value.match(reg));
})
</script>
39、断言匹配?<!以及应用
<body>
<p>
<a href="https://www.wujunjie.com/1.jpg">1.jpg</a>
<a href="https://oss.wujunjie.com/2.jpg">2.jpg</a>
<a href="https://cdn.wujunjie.com/3.jpg">3.jpg</a>
<a href="https://wujunjie.com/4.jpg">吴俊杰</a>
</p>
</body>
<script>
let hd = 'wujunjie2019nihao';
let reg = /(?<!\d+)[a-z]+/gi;
console.log(hd.match(reg));
const str = document.querySelector('p');
const newReg = /https:\/\/([a-z]+)?(?<!oss)\..+?(?=\/)/gi;
str.innerHTML.replace(newReg,(v) => {
console.log(v);
})
</script>
40、工作遇到的一些正则情况
let { questionText: { value, backupValue } } = needHandleContent.groups[0].questions[0];
const sentenceArr = value.match(/<sentence[\s\S]+?<\/sentence>/g);
try {
if (sentenceArr.length) {
sentenceArr.forEach(sentenceValue => {
sentenceValue.replace(/<sentence[\s\S]+?>([\s\S]+?)<\/sentence>/g, ($1, $2) => {
if ($1 && $2) {
const str =$2.replace(/(\?|\\|\/)/g, item => '\\' + item);
const reg = new RegExp(str, 'g');
backupValue = backupValue.replace(reg, (res) => {
if (res) {
return sentenceValue;
} else {
throw new Error();
}
})
} else {
throw new Error();
}
});
})
} else {
throw new Error();
}
} catch {
return this.$Message.warning('短文文本未能成功拼接,麻烦联系研发人员处理!');
}
const textTrim = (text):string => {
if(!text) return '';
var regex = /^( |\s)+|( |\s)+$/g;
text = text.replace(regex, '');
return text;
}
let startTags = item.match(/^<[^>]+>/);
let endTags = item.match(/<[^>]+>$/);
const regex = new RegExp('h(\\w+?)l(\\w+?)o', 'g')
const str = 'hello world, hello javascript, hello regex';
let match;
while((match = regex.exec(str)) !== null) {
console.log(match);
}
const text = '请以"前端开发、朋友"为话★2.请以"把握结构、我是测试考察点、观点态度"为考★3.写一段有100个单词的英文对★4.每段对话少于10★5.使用常见词代替长难词★6.使用短句代替长句★7.使用初中生能理解的单词'
const resArr = text.match(/[^★]+/g);
/(?<=:).+?(?=\))/
41、零个?多个?一个?