Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 - Fatal编程技术网

带一个星号的JavaScript正则表达式字符串

带一个星号的JavaScript正则表达式字符串,javascript,regex,Javascript,Regex,我想检查字符串是否有0…n个空格,1*和0…n个空格 模式 var derefpatt = new RegExp("\\s*\\*\\s*"); var res2 = derefpatt.test(string); 如果字符串为“**”,则为true,但只有当字符串为“**”时才应为true 怎么了 问候 Alex用于: 0…n空白->\s* one*->\* 0…n空白->\s* 您可以使用这个正则表达式(注意锚点^和$的用法): 另一方面,如果您想要相同数量的空间,可以使用

我想检查字符串是否有0…n个空格,1*和0…n个空格

模式

 var derefpatt = new RegExp("\\s*\\*\\s*");  
 var res2 = derefpatt.test(string); 
如果字符串为“**”,则为true,但只有当字符串为“**”时才应为true

怎么了

问候

Alex

用于:

  • 0…n空白
    ->
    \s*
  • one*
    ->
    \*
  • 0…n空白
    ->
    \s*
您可以使用这个正则表达式(注意锚点
^
$
的用法):

另一方面,如果您想要相同数量的空间,可以使用:

^(\s*)\*\1$

两个空格的n都一样吗?谢谢。。。var derefpatt=new RegExp(“^\\s*\*\\s*$”;作品但是为什么没有“^”和“$”的“\\s*\*\\s*”不起作用呢?因为RegExp.test()不检查整个字符串是否匹配,它会检查字符串中匹配的任何子字符串,如果找到任何子字符串,则返回true。由于星号之间有0个字符的空格,它满足星号后面有0个字符的空格的条件。
^(\s*)\*\1$