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


这个

"/^\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;
}

/^s*$/

是一个正则表达式对象

代码片段

/^s*$/.test(val)

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

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

如果您在本教程中查看此正则表达式,它将显示以下说明:

^ asserts position at start of the string
    s* matches any whitespace character (equal to [rntfv ])
      * 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* 

匹配零个或多个出现的空格字符

这里^

表示表达式的开始。

\s* 表示出现 0 个或多个空格字符(' '、制表符等)

$ 表示字符串的结尾。

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

/^s*$/

首先/{正则表达式在这里}/是你在这里写正则表达式的方式

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

\s 是任何字符串字符

"*"表示零或多

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

最新更新