Sunny-117/js-challenges

((2+3)+(3*4))+2---->['(2 + 3)+(3 * 4)', '2 + 3', '3 * 4']

Sunny-117 opened this issue · 0 comments

/**
 * fzq 4.23
 */
let str = '((2+3)+(3*4))+2'
function print(str) {
    const res = []
    matchRes = ''
    str = str.replace(/\(\(([\d\D]+)\)\)/g, function (match) {
        matchRes = match.substring(1, match.length - 1)
        res.push(matchRes)
    })
    matchRes.replace(/\([\s\S]+?\)/g, function (match) {
        // 非贪婪匹配
        res.push(match)
    })
    return res
}
console.log(print(str));// ['(2 + 3)+(3 * 4)',   '2 + 3',  '3 * 4']