简体   繁体   English

JavaScript中的正则表达式不仅不允许字段中包含空格,而且允许包含空格的字符串,还允许包含空字段

[英]Regular expression in javascript to not allow only whitespaces in a field but allow string with whitespaces and also allow empty field

I need regular expression to not allow only whitespaces in a field(the user should not enter just whitespaces in the field) but it can allow completely empty field and it can also allow string with whitespaces in between. 我需要正则表达式以不允许字段中仅包含空格(用户不应在字段中仅输入空格),但可以允许完全空白的字段,并且还可以允许字符串之间使用空格。

For example if I enter strings such as "This is a test" or "testing", the regular expression should pass validation. 例如,如果我输入诸如“ This is a test”或“ testing”之类的字符串,则正则表达式应通过验证。 Also, If I don't enter anything in the field, it should pass validation. 另外,如果我在该字段中未输入任何内容,则应通过验证。 It should fail if I enter only whitespaces. 如果我仅输入空格,它将失败。

I tried the below mentioned ones but they fail. 我尝试了以下提到的方法,但是它们失败了。

1) ^[^-\\s][a-zA-Z0-9_\\s-]+$ - doesn't allow me to enter string with whitespaces in between 1)^ [^-\\ s] [a-zA-Z0-9_ \\ s-] + $-不允许我输入介于中间的字符串

2) ^[^-\\s][\\w\\s-]+$ - doesn't allow me to enter a string 2)^ [^-\\ s] [\\ w \\ s-] + $-不允许我输入字符串

Are spaces allowed at the beginning of your string? 字符串开头是否允许空格?

If so, then something like that should work: 如果是这样,那么类似的事情应该起作用:

^$|.*\S+.*

Otherwise this one should do the trick: 否则,这应该可以解决问题:

^$|^\S+.*

Explanation: 说明:

I'll explain the version without leading spaces because the other one is just a slightly expanded version of it. 我将在不带空格的情况下解释该版本,因为另一个只是该版本的稍微扩展。

First, we check if the String is empty with ^$ . 首先,我们检查字符串是否为^$为空。 This is explained in another question here Regular expression which matches a pattern, or is an empty string . 这在此处的另一个问题中进行了说明,该正则表达式与模式匹配,或者为空字符串

^ marks the beginning of the string which we want to check for a match. ^标记了我们要检查匹配项的字符串的开头。

\\S+ checks for non-whitespace characters. \\S+检查非空白字符。 The + makes sure, that there needs to be at least one of them or more. +确保至少需要其中一个或多个。 This includes spaces, tabs, new-lines, etc. 这包括空格,制表符,换行符等。

.* matches any characters (denoted by the dot) and any number of them or none (denoted by the *). .*匹配任何字符(以点表示)和任意数量的字符,或不匹配任何字符(以*表示)。


This all assumes, that you need to match the whole string though. 所有这些都假定您需要匹配整个字符串。 If that is not the case for you, you could shorten the expression to: 如果不是这种情况,可以将表达式缩短为:

^$|\S+

or 要么

^$|^\S+

If the user input has matches for the first expression then you'll know the string he entered contains characters which are not space or is empty. 如果用户输入的内容与第一个表达式匹配,那么您将知道他输入的字符串包含非空格或空字符。 If it has matches for the second expression you additionally know that it starts with non-space characters. 如果它与第二个表达式匹配,则您还要知道它以非空格字符开头。

These expressions are of course only two of a lot of possible expressions. 这些表达式当然只是许多可能表达式中的两个。 I chose them because they are easy to read and explain(in my opinion at least). 我选择它们是因为它们易于阅读和解释(至少在我看来)。

Does it need to be regex? 是否需要使用正则表达式? If not, you could do: 如果没有,您可以这样做:

    if (userInput.length === 0 || (userInput.length > 0 && userInput.trim().length)) {
        console.log("user input ok");
    }

Where userInput is what the user has entered. 其中userInput是用户输入的内容。

userInput.trim() removes any white space from the beginning and end of the string. userInput.trim()删除字符串开头和结尾的所有空格。 So if the length of that is 0, that will be false and mean the string was only spaces. 因此,如果那的长度是0,那将是错误的,并且意味着该字符串只是空格。 Any spaces in between characters will be ok ( userInput.trim() leaves those alone) 字符之间的任何空格都可以( userInput.trim()那些空格)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM