接下来我们学习匹配字母之间的空格。
可以使用\s
搜寻空格,其中s
是小写。
此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与[\r\t\f\n\v]
类似。
var whiteSpace = "Whitespace. Whitespace everywhere!"
var spaceRegex = /\s/g;
console.log( whiteSpace.match(spaceRegex) ); // 返回 [" ", " "]
闯关:修改正则表达式countWhiteSpace
查找字符串中的多个空白字符。
var sample = "Whitespace is important in separating words";
var countWhiteSpace = /change/; // 修改这行
var result = sample.match(countWhiteSpace);
console.log(result);
正确代码
var sample = "Whitespace is important in separating words";
var countWhiteSpace = /\s+/g;
var result = sample.match(countWhiteSpace);
console.log(result);