使用字符的一个一个判断处理
const obj = { '{': "}", '(': ")", '[': "]", } function isValid(str) { const len = str.length let index = 0 if (len % 2 !== 0) { return false } const mid = len / 2 while (index < mid) { if (obj[str[index]] === str[len - 1 - index]) { index++ continue } if (obj[str[index]] === str[index + 1]) { index += 2 continue } return false } return true } console.log(isValid("[{]}"));