JavaScript正则表达式-如何检查字符串是否只包含字母、数字、破折号、下划线和点
我想检查字符串是否只包含特定类型的字符,字符是否都是字母,数字,点,下划线和破折号 如果字符串包含除我提到的字符之外的其他字符,那么它将导致假值 下面是一个关于正则表达式应该如何工作的示例:JavaScript正则表达式-如何检查字符串是否只包含字母、数字、破折号、下划线和点,javascript,regex,Javascript,Regex,我想检查字符串是否只包含特定类型的字符,字符是否都是字母,数字,点,下划线和破折号 如果字符串包含除我提到的字符之外的其他字符,那么它将导致假值 下面是一个关于正则表达式应该如何工作的示例: const regex = /regexpression/; const string1 = "abra-ibra_cadabra.2"; // Should console log true console.log(regex.test(string1)); const string2 =
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));
您可以按如下方式使用JavaScript test()方法:
const string1=“abra-ibra_cadabra.2”;
常量string2=“!abra ibra”;
const string3=“(abra)_ibra”;
设patt=/^(\w | \.\124;-)+$/;
console.log('string1:',patt.test(string1));
console.log('string2:',patt.test(string2));
console.log('string3:',patt.test(string3))代码>你必须带着最起码的东西、一段代码或详细的想法来到这里。我们不会为你做这项工作。顺便说一句,这个示例不起作用(语法错误)。请尝试此表达式/[\w\.-\d\]+/
,我们不会免费为您完成此工作!¡! 到目前为止,你尝试了什么,而不仅仅是你的伪代码,那甚至不是javascript代码->没有引号的字符串?
@johannchopin我想他忘了在wrong@Joseph:\d
和\u
已包含在\w
(\w | \.\124;-)+
[\w.-]+
感谢您提供的解决方案和解释