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

判断里面赋值就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")
}
posted @ 2025-01-21 16:18  彼时今日  阅读(10)  评论(0)    收藏  举报