编辑代码

const build = (code) => {
    return code
        .replace(/(\w\]*)(?=\[)/g, (path) => {
            if (/\w\]*/.test(path)) {
                return `${path}?.`
            }
            return path
        })
        .replace(/(?<=\])(\.)/g, (path) => {
            if (/\./.test(path)) {
                return `?${path}`
            }
            return path
        })

}

console.log('++code', build('a.b'))