gpt4 book ai didi

javascript - RegExp 测试函数奇怪的行为

转载 作者:行者123 更新时间:2023-12-01 01:23:26 25 4
gpt4 key购买 nike

我正在使用 javascript RegExp 检查字符是否为大写字母

function splitWords(text) {
const capReg = /[A-Z]/g;
const alphaNumReg = /[a-z0-9]/g;

for (let i = 0; i <= text.length - 1; i++) {
console.log(
text[i], text[i + 1], text[i + 2],
capReg.test(text[i]), capReg.test(text[i + 1]),
alphaNumReg.test(text[i + 2])
);
}
}
splitWords('ABCOption');

在预期的情况下C、O、p、true、true、true实际C、O、p、真、假、真

请帮我解决我做错的地方

最佳答案

这是获取数组并检查每个大写字母的方法:

const res = Array.from("ABCOption").map(e=>/[A-Z]/.test(e));

console.log(res)

关于javascript - RegExp 测试函数奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54039689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com