简体   繁体   English

Javascript正则表达式 - 开头没有空格+中间允许空格

[英]Javascript regex - no white space at beginning + allow space in the middle

I would like to have a regex which matches the string with NO whitespace(s) at the beginning.我想要一个正则表达式,它匹配开头没有空格的字符串。 But the string containing whitespace(s) in the middle CAN match.但是中间包含空格的字符串 CAN 匹配。 So far i have tried below到目前为止,我已经尝试过以下

[^-\s][a-zA-Z0-9-_\\s]+$

正则表达式可视化

Debuggex Demo调试器演示

Above is not allowing whitespace(s) at the beginning, but also not allowing in the middle/end of the string.以上不允许在开头有空格,也不允许在字符串的中间/结尾。 Please help me.请帮我。

In your 2nd character class, \\\\s will match \\ and s , and not \\s .在您的第二个字符类中, \\\\s将匹配\\s ,而不是\\s Thus it doesn't matches a whitespace.因此它不匹配空格。 You should use just \\s there.你应该在那里只使用\\s Also, move the hyphen towards the end, else it will create unintentional range in character class:另外,将连字符移到最后,否则它会在字符类中创建无意的范围:

^[^-\s][a-zA-Z0-9_\s-]+$

You need to use this regex:你需要使用这个正则表达式:

^[^-\s][\w\s-]+$
  • Use start anchor ^使用起始锚点^
  • No need to double escape \\s无需双重转义\\s
  • Also important is to use hyphen as the first OR last character in the character class.同样重要的是使用连字符作为字符类中的第一个或最后一个字符。
  • \\w is same as [a-zA-Z0-9_] \\w[a-zA-Z0-9_]

在开头使用 \\S

^\S+[a-zA-Z0-9-_\\s]+$

If you plan to match a string of any length (even an empty string) that matches your pattern and does not start with a whitespace, use (?!\\s) right after ^ :如果您打算以匹配符合你的格局不会有空白,开始使用任意长度(甚至是空字符串)的字符串(?!\\s)右后^

/^(?!\s)[a-zA-Z0-9_\s-]*$/
  ^^^^^^

Or, bearing in mind that [A-Za-z0-9_] in JS regex is equal to \\w :或者,请记住 JS 正则表达式中的[A-Za-z0-9_]等于\\w

/^(?!\s)[\w\s-]*$/

The (?!\\s) is a negative lookahead that matches a location in string that is not immediately followed with a whitespace (matched with the \\s pattern). (?!\\s)是一个否定前瞻,它匹配字符串中没有紧跟空格(与\\s模式匹配)的位置。

If you want to add more "forbidden" chars at the string start (it looks like you also disallow - ) keep using the [\\s-] character class in the lookahead:如果您想在字符串开头添加更多“禁止”字符(看起来您也不允许- ),请继续在前瞻中使用[\\s-]字符类:

/^(?![\s-])[\w\s-]*$/

To match at least 1 character, replace * with + :要匹配至少 1 个字符,请将*替换为+

/^(?![\s-])[\w\s-]+$/

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

 console.log(/^(?![\\s-])[\\w\\s-]+$/.test("abc def 123 ___ -loc- ")); console.log(/^(?![\\s-])[\\w\\s-]+$/.test(" abc def 123 ___ -loc- "));

This RegEx will allow neither white-space at the beginning nor at the end of.这个 RegEx 不允许在开头和结尾有空格。 Your string/word and allows all the special characters.您的字符串/单词并允许所有特殊字符。

^[^\s].+[^\s]$

This Regex also works Fine这个正则表达式也很好用

^[^\s]+(\s+[^\s]+)*$

试试这应该工作

[a-zA-Z0-9_]+.*$

/^[^.\\s]/ /^[^.\\s]/

  • try this instead it will not allow a user to enter character at first place试试这个,它不会允许用户首先输入字符
  • ^ matches position just before the first character of the string ^ 匹配字符串第一个字符之前的位置
  • . . matches a single character.匹配单个字符。 Does not matter what character it is, except newline除了换行符之外,它是什么字符无关紧要
  • \\s is space \\s 是空格

If your field for user name only accept letters and middle of space but not for begining and end如果您的用户名字段只接受字母和空格中间而不接受开头和结尾

User name: /^[^\\s][a-zA-Z\\s]+[^\\s]$/用户名: /^[^\\s][a-zA-Z\\s]+[^\\s]$/

If your field for user ID only accept letters,numbers and underscore and no spaces allow如果您的用户 ID 字段仅接受字母、数字和下划线且不允许有空格

user ID: /^[\\w]+$/用户名: /^[\\w]+$/

If your field for password only accept letters,number and special character no spaces allow如果您的密码字段只接受字母、数字和特殊字符,则不允许使用空格

Password: /^[\\w@#&]+$/密码: /^[\\w@#&]+$/

Note: \\w content a-zA-Z , number , underscore ( _ ) if you add more character, add you special character after \\w .注意: \\w内容a-zA-Znumber ,下划线( _ )如果添加更多字符,请在\\w之后添加特殊字符。

You can compare with user ID and password field in password field im only add some special character ( @#& ).您可以与密码字段中的用户 ID 和密码字段进行比较,我只添加一些特殊字符( @#& )。

India public thoko like 😁印度公众 thoko 喜欢 😁

I suggest below regex for this,为此,我建议使用以下正则表达式,

^[^\\s].*[^\\s]$

You can try regex in here你可以在这里尝试正则表达式

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

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