# JSON.minify (opens new window)

去掉json中的空白和注释 (设定注释为/* 块注释 */// 行注释

  • /"|(\/\*)|(\*\/)|(\/\/)|\n|\r|\[|]/g
    • g的‘迭代器’遍历匹配字符
    • RegExp.leftContextRegExp.rightContext判断当前匹配位置的上下文
    • 注:‘字符串’内符号皆保留

按自己的理解造个轮子:

JSON.minify = (json) => {
    // 特殊字符
    const r = /"|\/\*|\*\/|\/\/|\s/g
    /**
     * 0: 一般
     * 1: in string
     * 2: in block commit
     * 3: in line commit
     */
    let inState = 0
    let tmp
    let result = []
    let preIndex = 0

    while (tmp = r.exec(json)) {
        const match = tmp[0]
        if (inState < 2) {
            // 非特殊字符在非注释中,可直接使用
            result.push(RegExp.leftContext.substring(preIndex))
            // "和字符串中的特殊字符,可直接使用
            if (inState == 1 || match == '"') {
                result.push(match)
            }
        }
        preIndex = r.lastIndex
        if (match == '/*' && inState == 0) {
            inState = 2
        } else if (match == '*/' && inState == 2) {
            inState = 0
        } else if (match == '//' && inState == 0) {
            inState = 3
        } else if ((match == '\n' || match == '\r') && inState == 3) {
            inState = 0
        } else if (match == '"' && inState < 2) {
            // 字符串中的"
            if (!RegExp.leftContext.match(/\\$/)) {
                inState = inState == 1 ? 0 : 1
            }
        }
    }

    if (inState < 2) {
        result.push(json.substring(preIndex, json.length))
    }

    return result.join('')
}
最后更新: 1/12/2023, 1:44:05 PM