如何检测星座“*"在regex中

How to detect star sign "*" in regex?

本文关键字:quot regex 何检测 检测 星座      更新时间:2024-05-21

下面是我的代码。。

/(?!'*)/.test("test*test")

结果仍然返回true。

我想验证字符串,如果字符串中有*,它将返回false。

代码有问题吗?

正则表达式返回true,因为它与起始位置匹配。你的测试只是说"有没有一个位置后面没有*?"从字面上讲,任何字符串都会匹配——即使是"*"也会匹配,因为在*之后有一个位置没有后面有(另一个)*

如果您想测试字符串是否不包含*,最简单的解决方案是:

"test*test".indexOf("*") < 0 // true if no * in string

使用regex这样做类似于:

/^[^*]*$/.test("test*test")

但这是更多的工作。

简单地测试*的存在并否定输出

var string="123*456";
console.log(  !(/'*/.test(string))  );
false
var string="12*34*56";
console.log(  !(/'*/.test(string))  );
false
var string="123456";
console.log(  !(/'*/.test(string))  );
true