正则表达式为空字符串或空格 [英] Regex for empty string or white space

查看:1175
本文介绍了正则表达式为空字符串或空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测用户是否在文本框中输入空格:

I am trying to detect if a user enter whitespace in a textbox:

 var regex = "^\s+$" ; 
 if($("#siren").val().match(regex)) {
     echo($("#siren").val());
     error+=1;
    $("#siren").addClass("error");
    $(".div-error").append("- Champ Siren/Siret ne doit pas etre vide<br/>");
 }

if($(#siren)。 val()。match(regex))应该匹配空白字符串,但是,它看起来不起作用,我做错了什么?

if($("#siren").val().match(regex)) is supposed to match whitespace string, however, it doesn' t seems to work, what am I doing wrong ?

感谢您的帮助

推荐答案

\ .match 调用中的反斜杠)未正确转义。但是,使用正则表达式文字会更容易。要么工作:

The \ (backslash) in the .match call is not properly escaped. It would be easier to use a regex literal though. Either will work:

var regex = "^\\s+$";
var regex = /^\s+$/;

另请注意 + 至少需要一个空间。您可能想要使用 *

Also note that + will require at least one space. You may want to use *.

这篇关于正则表达式为空字符串或空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆