js条件判断的类python海象写法

posted @

判断里面赋值就OK啦

var ss = "hi go oh"
const reg = /\w{2}/g 

const ii = ss.matchAll(reg)
while (_ = ii.next()) {
    if (_.done) break
    console.log(_.value, _.done)
    ss = ss.replace(_.value[0], "xx")
}

// console.log(_)

console.log(ss)

不过,这有个小小的变量作用域溢出问题

输出结果:

[ 'hi', index: 0, input: 'hi go oh', groups: undefined ] false
[ 'go', index: 3, input: 'hi go oh', groups: undefined ] false
[ 'oh', index: 6, input: 'hi go oh', groups: undefined ] false
xx xx xx  

甚至于可以像下面这样简写

// 套一层,就可以取值进一步判断了
while (!(_ = ii.next()).done) {
    // if (_.done) break
    console.log(_.value, _.done)
    ss = ss.replace(_.value[0], "xx")
}
转载请注明出处@https://www.cnblogs.com/noah227/p/18511208
posted @   彼时今日  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示