这种符号是什么" / ^ \ s * $ / .test(val)"意思

时间:2017-01-07 18:58:36

标签: javascript

这是什么" / ^ \ s * $ /"这意味着我试图从这里学习它:https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions但是不能理解它吗?



fn: function (val) {
  return typeof val === 'string' ? 
  !/^\s*$/.test(val) : val !== undefined && val !== null;
}




3 个答案:

答案 0 :(得分:2)

/^\s*$/

RegExp object

代码段

/^\s*$/.test(val)

使用RegExp test方法测试字符串val是空还是仅包含空格。来自文档:

  

test()方法执行搜索常规之间的匹配   表达式和指定的字符串。返回true或false。

如果您在 this tutorial中查看此正则表达式,它会显示以下说明:

^ asserts position at start of the string
    \s* matches any whitespace character (equal to [\r\n\t\f\v ])
      * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

基本上,它意味着:

/^...$/ 

匹配从开头到结尾的字符串,

\s* 

匹配零个或多个空格字符

答案 1 :(得分:2)

此处 ^ 表示表达的开头。

\ s * 表示出现0个或更多空格字符('',标签等)

$ 表示字符串的结尾。

所以/^\s*$/是空字符串或字符串的正则表达式,只有空格。

答案 2 :(得分:0)

/^\s*$/

第一个/ {regex在这里} /是你在这里写regex的方式

^ {somethingelse} $意思是在中间正则表达式中开始并结束

\ s是任何字符串字符

' *'意味着零或更多

所以它意味着所有元素都是字符,而不是数字或符号或空格