简体   繁体   English

正则表达式的客户端验证 - 允许在中间但不是开始和结束的空格

[英]Clientside validation for regex - allowing whitespace in middle but not beginning and end

I'm allowing whitespace for this regex like this: 我允许这个正则表达式的空格像这样:

javascript: JavaScript的:

var re_username = /^[a-zA-Z\040\.\-]+$/;
var is_username = re_username.test($('#chatusername').val());
console.log(is_username);   

... but I want to disallow it at the beginning and at the end. ......但我想在开始和结束时不允许它。 This worked: 这有效:

javascript: JavaScript的:

var re_username = /^[a-zA-Z]+[a-zA-Z\040\.\-]+[a-zA-Z\.]+$/;

... but I need to enter at least 3 (<3 digits returns false although I enter eg A) digits for the regex to return true which causes trouble adding classes and stuff ... ...但是我需要输入至少3个(<3位数字返回false,尽管我输入例如A)数字,正则表达式返回true,这会导致添加类和东西的麻烦......

I've found this for this allowing whitespaces at the beginning and end 我发现这个允许开头和结尾的空格

javascript: JavaScript的:

var re_username = /^\S+(?: \S+)*$/

But it's allowing all characters ... . 但它允许所有角色...... How can I match my targeted regex? 我如何匹配我的目标正则表达式? I tried this ... but it already didn't work .. 我试过这个...但它已经无法正常工作..

var re_username = /^\S+([a-zA-Z]\040\S+)*$/; 

Use 使用

var re_username = /^[a-zA-Z.-]+(?:\040+[a-zA-Z.-]+)*$/;

It will match 它会匹配

  • ^ - start of string ^ - 字符串的开头
  • [a-zA-Z.-]+ - 1+ letters, . [a-zA-Z.-]+ - 1+个字母, . and - 而且-
  • (?:\\040+[a-zA-Z.-]+)* - 0+ sequences of (?:\\040+[a-zA-Z.-]+)* - 0+序列
    • \\040+ - 1+ regular spaces \\040+ - 1 +个常规空间
    • [a-zA-Z.-]+ - 1+ letters, . [a-zA-Z.-]+ - 1+个字母, . and - 而且-
  • $ - end of string $ - 结束字符串

See the regex demo . 请参阅正则表达式演示

NOTE: if you do not want to allow consecutive multiple spaces, remove the + after \\040 . 注意:如果您不想允许连续多个空格,请在\\040之后删除+

Another, less efficient way, is via lookaheads: 另一种效率较低的方法是通过前瞻:

var re_username = /^(?!\040|.*\040$)[a-zA-Z\040.-]+$/;
                    ^^^^^^^^^^^^^^^ 

Here, the (?!\\040|.*\\040$) negative lookahead will fail the match if a space is found right at the start of a string or after any 0+ chars ( .* ) at the end of the string ( $ ). 如果在字符串的开头找到一个空格,或者在字符串末尾的任何0+字符( .* )后找到空格,则(?!\\040|.*\\040$)否定前瞻将使匹配失败( $ )。

暂无
暂无

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

相关问题 不允许使用Java验证进行空格 - Not Allowing Whitespace With Javascript Validation 在字符串的开头和结尾查找特殊字符和空格的正则表达式是什么? - What is the regex for finding special characters and whitespace at the beginning AND end of the string? 正则表达式unicode字母的客户端验证 - Clientside validation for regex unicode letters Bootstrap Typeahead正则表达式-允许空格 - Bootstrap Typeahead Regex — Allowing whitespace 字符串替换正则表达式,开头是空格 - String Replace Regex, Whitespace in the Beginning 正则表达式 - 或 _ 在字符串的开头或结尾 - Regex for - or _ at beginning or end of string 正则表达式在开头和结尾没有空格,但允许所有特殊字符、空格、小写和大写字母以及介于两者之间的数字 - RegEx for no whitespace at the beginning and end but allows all special characters , spaces , lowercase and uppercase alphabet and numbers in between 以html格式自定义客户端正则表达式验证 - Custom clientside regex validation in an html form 在RegEx中,我只需要在中间允许空格,并在开头和结尾处防止空格 - In a RegEx I need to allow spaces only in middle and prevent spaces at beginning and end 正则表达式-右/左修剪+在中间减少一个以上的空间到一个+在开头和结尾处删除换行符,如果在中间&gt; 2,则减少到两个 - Regex - Right/Left trim + reduce more than one space in middle to one + remove line break at beginning & end incl reduction to two if in middle & >2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM