鍍金池/ 問答/Java  Android  HTML/ 正則匹配字符串中有\(zhòng)n的時(shí)候出錯(cuò),但是從瀏覽器input框輸入就可以通過判斷,瀏

正則匹配字符串中有\(zhòng)n的時(shí)候出錯(cuò),但是從瀏覽器input框輸入就可以通過判斷,瀏覽器轉(zhuǎn)義了嗎?還有為什么字符串中有\(zhòng)n會出錯(cuò)?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1">
    <title></title>
    <style></style>
</head>

<body>
    <input type="text" id="text" >
    <script>
        window.onload = function(){
            var reg = /^(?=.*\d)(?=.*[a-zA-Z]).{4,}$/;
            console.log(reg.test('1212aa1a1a11a1\n111'));
            document.getElementById('text').onkeyup =function(){
                var val =this.value;
                console.log(reg.test(val));
            }
        }
    </script>
</body>
</html>
回答
編輯回答
瘋子范

是轉(zhuǎn)義字符, n是換行的意思,
'1212aa1a1a11a1\\n111', 這里要加一個(gè)\
自動轉(zhuǎn)義是肯定存在的

2018年3月30日 08:02