Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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_If Statement_Punctuation - Fatal编程技术网

在javascript中检查字符串中是否只有标点符号

在javascript中检查字符串中是否只有标点符号,javascript,if-statement,punctuation,Javascript,If Statement,Punctuation,在if语句中,我想检查字符串中是否有标点符号。如果它有标点符号,我希望弹出一条警告消息 if((/*regex presumably*/.test(rspace))==true||(/*regex presumably*/.test(sspace))==true){ alert('Please enter your guessed answer in the fields provided, without punctuation marks.'); //if only pun

在if语句中,我想检查字符串中是否有标点符号。如果它有标点符号,我希望弹出一条警告消息

if((/*regex presumably*/.test(rspace))==true||(/*regex presumably*/.test(sspace))==true){
    alert('Please enter your guessed answer in the fields provided, without punctuation marks.');

    //if only punctuation are entered by the user, an alert message tells them to enter something.
}
我需要一个正则表达式,还是有一个自动的方法来为我这样做? 提前谢谢

您不需要正则表达式,但这可能是最简单的。这是一个直截了当的字符类,将不允许使用的字符放在
[…]
中,例如:

if (/[,.?\-]/.test(rspace)) {
    // ....
}
请注意,在
[]
中,
-
是特殊的,因此如果要包含它,请在其前面加一个反斜杠(如上所述)。反斜杠也一样,来吧



请注意,您永远不需要在条件中写入
==true
。如果您愿意,您可以这样做,但从JavaScript引擎的角度来看,它没有任何用处。

这可能会有所帮助:关于[]或{}或()呢?它们也很特别吗?@StephenGevanni:最简单的事情就是检查一些文档。很难读,但是很好。在字符类中,列表中只有一个字符是特殊的:
]
(当它结束类时)。其他人都不是。不过,其他角色在角色类之外是特殊的。