在 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 引擎的 Angular 来看,它没有任何作用。
关于javascript - 在javascript中检查字符串中是否只有标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603588/