#### 阅读更多系列文章请访问我的[GitHub 博客](https://github.com/chencl1986/Blog) 原题链接:[https://leetcode-cn.com/problems/valid-parentheses/](https://leetcode-cn.com/problems/valid-parentheses/) 解题思路: 1. 进行while循环,每循环一次替换一对括号。 2. 如果发现替换之后,替换前后的字符串相等,表示有无效括号。 3. 如果替换到字符串为空,表示所有括号都有效。 ```javascript [] /** * @param {string} s * @return {boolean} */ var isValid = function (s) { while (s.length) { const temp = s; // 缓存替换前字符串 s = s.replace(/(\(\))|(\[\])|(\{\})/, ''); // 如果替换后字符串与替换前相同,说明存在不成对括号 if (s === temp) { return false; } } // 如果数组为空退出循环,则字符串内都为有小括号 return true; }; ```