RegExp: /\s/g 和 /\s+/g的差異?

這篇說明/\s/g 和 /\s+/g的差異,

Is there a difference between /\s/g and /\s+/g?
http://stackoverflow.com/questions/5964373/is-there-a-difference-between-s-g-and-s-g


擷取重點如下:

\s : means "one space"

\s+ : means "one or more spaces"


var str = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                // ##A#B##C###D#EF#
console.log(str.replace(/\s+/g, '#'));                             // #A#B#C#D#EF#  --> 代表1到多個空白, 都是以一個"#"取代。







留言

熱門文章