目录1、异常案例:2、原因分析3、解决方法13、解决方法23、解决方法3总结1、异常案例: 使用正则匹配111 const regular = /111/g; // 匹配111
使用正则匹配111
const regular = /111/g; // 匹配111
// console.log(regular.test('111')); // true 匹配成功
// console.log(regular.test('111,111')); // true 匹配成功
const list = [
'111',
'111',
'111,111',
'111,111'
];
list.forEach((element, index) => {
// 异常写法
console.log('log_________' + regular.test(element));
});
// 打印结果
// log_________true
// log_________false // 会存在正则匹配为false的异常
// log_________true
// log_________true
why? 首先去MDN看了看正则的基础概念。
发现了lastIndex 这个属性。
用正则表达式只要设置了全局匹配标志 /g ,test()方法 的执行就会改变正则表达式 lastIndex 属性。再循环中连续的执行 test() 方法,后面的执行将会从 lastIndex 数字处开始匹配字符串。
原来如此,看来test() 方法确实也不能随便滥用。
确认验证一下:
const regular = /111/g; // 匹配111
const list = [
'111',
'111',
'111,111',
'111,111'
];
list.forEach((element, index) => {
// 异常写法
console.log('log_________' + regular.test(element));
// 打印lastIndex
console.info('logLastIndex___' + regular.lastIndex);
});
// 打印结果
// log_________true
// logLastIndex___3
// log_________false // 确实因为lastIndex为3导致的
// logLastIndex___0
// log_________true
// logLastIndex___3
// log_________true
// logLastIndex___7
上面我们发现正则test()方法有lastIndex属性,每次循环给恢复一下。
const regular = /111/g; // 匹配111
const list = [
'111',
'111',
'111,111',
'111,111'
];
list.forEach((element, index) => {
regular.lastIndex = 0;
console.log('log_________' + regular.test(element)); // 打印正常
});
问题解决 OK了。
上面我们发现正则表达式设置了全局标志 /g 的问题,去掉/g全局匹配。(这个具体还得看自己的应用场景是否需要/g)
const regular = /111/; // 去掉/g全局匹配
const list = [
'111',
'111',
'111,111',
'111,111'
];
list.forEach((element, index) => {
console.log('log_________' + regular.test(element)); // 打印正常
});
OK了。
js有基本数据类型和引用数据类型
ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型。\
基本数据类型:Number、String、Boolen、Undefined、Null、Symbol、Bigint。\
引用数据类型:也就是对象类型Object type,比如:对象(Object)、数组(Array)、函数(Function)、日期(Date)、正则表达式(RegExp)。
so正则表达式属于引用数据类型,按传统思维肯定是需要“深拷贝”的,需要new 一个新Object。
const regular = /111/g;
const list = [
'111',
'111',
'111,111',
'111,111'
];
list.forEach((element, index) => {
// 正确写法 new RegExp的内存指向在循环过程中每次都单独开辟一个新的“对象”,不会和前几次的循环regular.test(xxx)改变结果而混淆
// console.log('log_________' + /111/g.test(element)); // 这样写当然也行
console.log('log_________' + new RegExp(regular).test(element)); // 打印OK了
});
OK了。
到此这篇关于js循环中使用正则失效异常的踩坑的文章就介绍到这了,更多相关js循环中正则失效异常内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: js循环中使用正则失效异常的踩坑实战
本文链接: https://www.lsjlt.com/news/212782.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0