简体   繁体   English

正则表达式允许字母数字和空格,但不允许连续两个空格

[英]RegEx to allow letters numbers and spaces but not two spaces in a row

Hello I have a RegEx that allows up to 18 characters with numbers, letters and spaces only.您好,我有一个 RegEx,最多允许 18 个字符,仅包含数字、字母和空格。

How can I modify this to check if the string is starting with a space, or has two spaces in a row?如何修改它以检查字符串是否以空格开头,或者是否连续有两个空格?

/^[0-9A-Za-z\s]{1,18}$/

I am using this regex in both native javascript (not jquery) and PHP.我在本机 javascript(不是 jquery)和 PHP 中使用这个正则表达式。

您可以使用否定前瞻来检查字符串是否以空格开头或是否有 2 个或更多连续空格,例如:

^(?!.*\s{2,})(?!^ )[0-9A-Za-z\s]{1,18}$
(?:(?![ ]{2}).)+

use this regex.使用这个正则表达式。 This may solve your problem.这可能会解决您的问题。

暂无
暂无

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

相关问题 javascript正则表达式,以允许字母,数字,句点,连字符,下划线和空格 - javascript regex to allow letters, numbers, periods, hyphens, underscores, and spaces 用于名称验证的正则表达式只允许字母和空格 - Regex for names validation allow only letters and spaces javascript:使用RegEx允许字母,数字和空格(至少包含一个字母和数字) - javascript: Use RegEx to allow letters, numbers, and spaces (with at least one letter and number) RegEx字符串,用于三个字母和两个带前置和后置空格的数字 - RegEx string for three letters and two numbers with pre- and post- spaces 正则表达式:允许字母,数字和空格(至少有一个字母不是数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number) 正则表达式:允许字母,数字和空格(至少包含一个字母或数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number) RegEx for Javascript(替换功能)仅允许使用字母和空格,而不能以空格开头? - RegEx for Javascript (replace function) to allow only letters and spaces, but not start with space? 正则表达式从字符串中提取两个带空格的数字 - Regex to extract two numbers with spaces from string javascript 使用正则表达式测试字母、数字、空格和破折号 - javascript use test with regex for letters, numbers, spaces and dash 只允许字母、数字和空格。 用单个空格替换重复的空格 - allow only letters, numbers, and spaces. replace repeated spaces with single space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM