Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不按给定条件过滤的正则表达式_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 不按给定条件过滤的正则表达式

Javascript 不按给定条件过滤的正则表达式,javascript,jquery,regex,Javascript,Jquery,Regex,我想找出给定的单词是否不包含:|“。因此我使用下面的javascript代码块来查找它。但它接受所有包含这些字符的值,而这些字符也不包含 $(document).ready(function() { $('#chkResult').click(function() { $('#resultDiv').text(/[^:<>\|"]+/.test($('#dataText').val())); }); });​ $(文档).ready(函数(){ $(

我想找出给定的单词是否不包含<>:|“。因此我使用下面的javascript代码块来查找它。但它接受所有包含这些字符的值,而这些字符也不包含

$(document).ready(function() {
    $('#chkResult').click(function() {
        $('#resultDiv').text(/[^:<>\|"]+/.test($('#dataText').val()));
    });
});​
$(文档).ready(函数(){
$('#chkResult')。单击(函数(){
$('resultDiv').text(/[^:\\\\“]+/.test($('dataText').val());
});
});​
只需锚定正则表达式:

$(document).ready(function() {
    $('#chkResult').click(function() {
        $('#resultDiv').text(/^[^:<>\|"]+$/.test($('#dataText').val()));
//                      here__^    here__^
    });
});​
$(文档).ready(函数(){
$('#chkResult')。单击(函数(){
$('resultDiv').text(/^[^:\'124;“]+$/.test($('dataText').val());
//在这里__^
});
});​

您必须设置regex,以便使用
^
$
从开始到结束测试字符串:

/^[^:<>\|"]+$/.test($('#dataText').val())
/^[^:\\\;“]+$/.test($('\\\'dataText').val())

否则,如果测试字符串至少包含一个与您的组不匹配的字符,则测试通过。

我已在该链接中尝试过。您知道如果不锚定,为什么会出现这种行为吗?对我来说毫无意义。