我正在调试某人的代码,想知道此正则表达式的作用是什么?
代码:
<script>
var t = document.getElementById("filterVal").value;
var s = filterVal.replace(/"([^"]+(?="))"/g, '$1')
</script>
答案 0 :(得分:4)
它删除了双引号的平衡计数。
console.log('a"bc"de"f'.replace(/"([^"]+)(?=")"/g, '$1'));
console.log('a"bc"d"e"f'.replace(/"([^"]+)(?=")"/g, '$1'));
// a version without a positive lookahead (looks like the same result)
console.log('a"bc"de"f'.replace(/"([^"]+)"/g, '$1'));
console.log('a"bc"d"e"f'.replace(/"([^"]+)"/g, '$1'));
答案 1 :(得分:1)
如前所述,基本上消除了qoutes:说filteVal =“今天天气晴朗”
var t = document.getElementById("filterVal").value;
var s = filterVal.replace(/"([^"]+(?="))"/g, '$1')
//output: It's sunny today
您也可以通过右键单击chrome并打开inspect并在控制台中键入它来进行尝试。