正则表达式,检测字符串中没有空格 [英] Regex, detect no spaces in the string

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

问题描述

这是我目前的正则表达式检查:

This is my current regex check:

const validPassword =(password)=> password.match(/^(?=.* \d)(= \S?){6} $ /)(= * [A-ZA-Z]);

我检查了至少1个字母和1个数字以及至少6个字符的长度。但是我也想确保字符串中的任何地方都没有空格。

I have a check for at least 1 letter and 1 number and at least 6 characters long. However I also want to make sure that there are no spaces anywhere in the string.

到目前为止,我可以输入包含空格的6个字符串:(< a href =https://i.stack.imgur.com/tpPBh.png =nofollow noreferrer>

So far I'm able to enter in 6 character strings with spaces included :(

在此处找到此答案,但由于某些原因,在我的代码中它正在传递。

Found this answer here, but for some reason in my code it's passing.

匹配的正则表达式是什么,文本之间不包含空格?

推荐答案

好像你需要

/^(?=.*\d)(?=.*[a-zA-Z])\S{6,}$/

详细信息


  • ^ - 字符串开头

  • (?=。* \ d) - 1位数(至少)

  • (?=。* [a-zA-Z]) - 至少1个字母

  • \S {6,} - 6个或更多非空白字符

  • $ - 结束字符串锚点

  • ^ - start of string
  • (?=.*\d) - 1 digit (at least)
  • (?=.*[a-zA-Z]) - at least 1 letter
  • \S{6,} - 6 or more non-whitespace chars
  • $ - end of string anchor

使用对比原则,你可以将模式改为

With a principle of contrast in mind, you may revamp the pattern into

/^(?=\D*\d)(?=[^a-zA-Z]*[a-zA-Z])\S{6,}$/

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

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