zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Invert Regular Expression Matches with JavaScript #273

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

/s可以匹配空白字符。 /S可以匹配任何非空白字符,计算出所有feiko非空白字母的个数。

方法

/S/g

练习

Use /\S/g to count the number of non-whitespace characters in testString.

代码


// Setup
var testString = "How many non-space characters are there in this sentence?";

// Only change code below this line.

var expression = /\S/g;  // Change this line

// Only change code above this line

// This code counts the matches of expression in testString
var nonSpaceCount = testString.match(expression).length;

结果显示

image

来源

https://www.freecodecamp.org/challenges/invert-regular-expression-matches-with-javascript

zilongxuan001 commented 6 years ago

代码为


// Setup
var testString = "How many";

// Only change code below this line.

var expression = /\S/g;  // Change this line

// Only change code above this line

// This code counts the matches of expression in testString
var nonSpaceCount = testString.match(expression).length;

结果为 image

算出来的是字母的个数,不是单词的个数。