我想检查一个字符串是否只包含特定类型的字符,并且字符都是字母、数字、点、下划线和破折号。
如果字符串包含除我提到的字符之外的其他字符,那么它应该导致错误值。
以下是有关正则表达式如何工作的示例:
const regex = /regexpression/;
const string1 = "abra-ibra_cadabra.2";
// Should console log true
console.log(regex.test(string1));
const string2 = "!abra-ibra";
// Should console log false
console.log(regex.test(string2));
const string3 = "(abra)_ibra";
// Should console log false
console.log(regex.test(string3));
Run Code Online (Sandbox Code Playgroud)