Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 允许标点符号和单词之间空格的正则表达式_Javascript_Regex_Alphanumeric_Punctuation - Fatal编程技术网

Javascript 允许标点符号和单词之间空格的正则表达式

Javascript 允许标点符号和单词之间空格的正则表达式,javascript,regex,alphanumeric,punctuation,Javascript,Regex,Alphanumeric,Punctuation,我想要一个正则表达式,它可以防止出现空格,并且只允许使用带有标点符号的字母和数字(西班牙语)。下面的正则表达式工作得很好,但它不允许标点符号 ^[a-zA-Z0-9_]+( [a-zA-Z0-9_]+)*$ 例如,使用此正则表达式时,“Hola como estas”可以,但“Hola,como estas?”不匹配 如何将其调整为标点符号?使用\W+而不是空格,并在末尾添加\W*: /^[a-zA-Z0-9_]+(?:\W+[a-zA-Z0-9_]+)*\W*$/ 看 解释

我想要一个正则表达式,它可以防止出现空格,并且只允许使用带有标点符号的字母和数字(西班牙语)。下面的正则表达式工作得很好,但它不允许标点符号

^[a-zA-Z0-9_]+( [a-zA-Z0-9_]+)*$
例如,使用此正则表达式时,“Hola como estas”可以,但“Hola,como estas?”不匹配


如何将其调整为标点符号?

使用
\W+
而不是空格,并在末尾添加
\W*

/^[a-zA-Z0-9_]+(?:\W+[a-zA-Z0-9_]+)*\W*$/

解释

                         EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-zA-Z0-9_]+            any character of: 'a' to 'z', 'A' to 'Z',
                           '0' to '9', '_' (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \W+                      non-word characters (all but a-z, A-Z, 0-
                             9, _) (1 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
    [a-zA-Z0-9_]+            any character of: 'a' to 'z', 'A' to
                             'Z', '0' to '9', '_' (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  \W*                      non-word characters (all but a-z, A-Z, 0-
                           9, _) (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

谢谢你的帮助。但我只是在angular Validator中测试了这个模式,它好像没有读取整个模式,结果验证错误。在这种情况下,你建议我怎么做@Ryszard@JoseAguirre请将您使用的代码添加到问题中,以查看可能出现的错误。@JoseAguirre请确保不要将
/^[a-zA-Z0-9\]+(?:\W+[a-zA-Z0-9\]+)*\W*$/
用引号括起来。谢谢,错误是由引号引起的,因为我将模式添加到组件内的属性中,以避免重复模式。但是直接在Validators.pattern()中添加模式效果很好@理夏德