var testString = "How many spaces are there in this sentence?";
var expression = /\s+/g;
var spaceCount = testString.match(expression).length;
我們也可以使用正則表達式選擇器 \s
來查找一個字符串中的空白。
空白字符有 " "
(空格符)、\r
(回車符)、\n
(換行符)、\t
(制表符) 和 \f
(換頁符)。
空白正則表達式類似於:
/\s+/g