jQuery 空字符串或空格的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19121375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for empty string or white space
提问by dtjmsy
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))
is supposed to match whitespace string, however, it doesn' t seems to work, what am I doing wrong ?
if($("#siren").val().match(regex))
应该匹配空白字符串,但是,它似乎不起作用,我做错了什么?
Thanks for your helps
感谢您的帮助
回答by Explosion Pills
The \
(backslash) in the .match
call is not properly escaped. It would be easier to use a regex literal though. Either will work:
调用中的\
(反斜杠).match
没有正确转义。不过,使用正则表达式文字会更容易。要么会起作用:
var regex = "^\s+$";
var regex = /^\s+$/;
Also note that +
will require at least one space. You may want to use *
.
另请注意,这+
至少需要一个空格。您可能想要使用*
.
回答by asantaballa
If you are looking for empty string in addition to whitespace you meed to use * rather than +
如果您正在寻找除空格之外的空字符串,您可以使用 * 而不是 +
var regex = /^\s*$/ ;
^
回答by yan
If you're using jQuery, you have .trim()
.
如果您使用 jQuery,那么您有.trim()
.
if ($("#siren").val().trim() == "") {
// it's empty
}
回答by JustGage
This is my solution
这是我的解决方案
var error=0;
var test = [" ", " "];
if(test[0].match(/^\s*$/g)) {
$("#output").html("MATCH!");
error+=1;
} else {
$("#output").html("no_match");
}
回答by Jose Paez
Had similar problem, was looking for white spaces in a string, solution:
有类似的问题,正在寻找字符串中的空格,解决方案:
To search for 1 space:
var regex = /^.+\s.+$/ ;
example: "user last_name"
To search for multiple spaces:
var regex = /^.+\s.+$/g ;
example: "user last name"
搜索 1 个空间:
var regex = /^.+\s.+$/ ;
例如:“用户姓氏”
要搜索多个空间:
var regex = /^.+\s.+$/g ;
例如:“用户姓氏”
回答by Chris S.
One important note when talking about scripting and whitespaces: Browsers parsing spaces, whitespaces, tabs and linebreaks to spaces does mean that they actually ARE a whitespace. Whitespaces themself are \xA0
(Char 160). I.e. often if you write PHP files on a mac and magically don't only hit space, you have a char that looks like a space but aint one. To remove that and make the magical Error 500 that suddenly starts to come up disappear, remove all \xA0
(Char 160)'s from your script. (When searching, in TextWrangler / BBEdit you'd hit the checkbox "Grep" in the search dialog - should be similar in Coda, Eclipse etc). It takes some time to figure out. Even if it does not 100% match the topic, it's the first result on google when you search for exactly that problem. Hope it helps someone.
谈论脚本和空格时的一个重要注意事项:浏览器将空格、空格、制表符和换行符解析为空格确实意味着它们实际上是空格。空格本身是\xA0
(Char 160)。即,如果您经常在 Mac 上编写 PHP 文件并且神奇地不仅命中空格,您还有一个看起来像空格但不是空格的字符。要删除它并使突然开始出现的神奇错误 500 消失,请\xA0
从脚本中删除所有(Char 160)。(搜索时,在 TextWrangler / BBEdit 中,您会在搜索对话框中选中“Grep”复选框 - 在 Coda、Eclipse 等中应该类似)。需要一些时间来弄清楚。即使它不是 100% 匹配主题,当您搜索该问题时,它也是 google 上的第一个结果。希望它可以帮助某人。
回答by marcelocra
If one only cares about whitespace at the beginning and end of the string (but not in the middle), then another option is to use String.trim():
如果只关心字符串开头和结尾(而不是中间)的空格,那么另一种选择是使用 String.trim():
" your string contents ".trim();
// => "your string contents"