Javascript正则表达式不匹配-包含完整代码

Javascript regex not matching - full code included

本文关键字:代码 -包 正则表达式 不匹配 Javascript      更新时间:2023-09-26

我之前发布了这个查询,并得到了包含的响应,但我似乎无法使这个查询匹配——我使用的是Firefox。我到底错过了什么?(应为可复制/可粘贴)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
    </head>
    <body>
            <script type="text/javascript">
                    $('#inp1').on('keyup',function(){
                            $('#out1').val($(this).val().match(/(?=.*[a-z])(?=.*[A-Z])(?=.*'d)(?=.*([^'w]|_)).{8,}/));
                    })
            </script>
            Inp <input id="inp1" type="text" value="fDe^je872Fhdj"><br>
            Out <input id="out1" />        
    </body>
    </html>

match()返回Array。使用索引获取第一个也是唯一的值,这就是整个匹配。

$(this).val().match(/(?=.*[a-z])(?=.*[A-Z])(?=.*'d)(?=.*([^'w]|_)).{8,}/)[0]);
                                          // index      <-----------------^

并在DOM ready中使用您的代码,因为此时元素还不存在。

$(document).ready(function() {
    $('#inp1').on('keyup', function() {
        $('#out1').val($(this).val().match(/(?=.*[a-z])(?=.*[A-Z])(?=.*'d)(?=.*([^'w]|_)).{8,}/)[0]);
    })
});