Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 Check是一个字符串,而不是使用regex的其他单词_Javascript_Regex Negation - Fatal编程技术网

Javascript Check是一个字符串,而不是使用regex的其他单词

Javascript Check是一个字符串,而不是使用regex的其他单词,javascript,regex-negation,Javascript,Regex Negation,我有一个例子,我需要在javascript中使用正则表达式查找字符串是否与单词完全不匹配 我试着消极地向前看 var reg = /(?!(^Hello$))/ var a = "Hello"; var b = "something else"; console.log(reg.test(a)) // I need this to be false console.log(reg.test(b)) // I need this to be true 我怎样才能做到这一点?在Javascri

我有一个例子,我需要在javascript中使用正则表达式查找字符串是否与单词完全不匹配

我试着消极地向前看

var reg = /(?!(^Hello$))/
var a = "Hello";
var b = "something else";


console.log(reg.test(a)) // I need this to be false
console.log(reg.test(b)) // I need this to be true

我怎样才能做到这一点?在Javascript中,控制台日志都给出了true,问题是您没有锚定整个regexp,因此它可以在输入字符串中的任何点匹配。它不会在开头匹配,但会在
'H'
之后匹配,因为没有字符串匹配
(?!(^Hello$)
,而
'H'
之后是
,因此满足了负面展望

要使您的regexp执行您想要的操作,请锚定它:

var reg = /^(?!Hello$)/;

如果我错了,请更正我的可能副本,但您不能在这里使用字符串相等吗?这必须使用正则表达式来完成。谢谢。。这很有效。我们有一个配置文件,在其中指定正则表达式来验证输入。另一种方法是添加新的验证器,如“notmatch”。我试图用现有的正则表达式来实现这一点。@ PRACKASH,如果这需要使用用户输入的数据,你需要考虑如何在匹配的字符串中逃脱任何正则表达式特殊字符。例如,如果他们想匹配“非
$0.00
”,该怎么办?我认为实现一个不使用regex的notequals会更快、更可靠。