Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_Regex - Fatal编程技术网

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.-]+
感谢您提供的解决方案和解释