Skip to content

Latest commit

 

History

History
30 lines (19 loc) · 1.01 KB

remove-whitespace.md

File metadata and controls

30 lines (19 loc) · 1.01 KB
标题 标签
removeWhitespace(移除字符串的空白) string,regexp(字符串,正则表达式)

返回删除了空格的字符串。

  • 将 String.prototype.replace() 与正则表达式结合使用,将所有出现的空白字符替换为空字符串。
const removeWhitespace = str => str.replace(/\s+/, '');

调用方式:

removeWhitespace('Lorem ipsum.\n Dolor sit amet. ');
// 'Loremipsum.Dolorsitamet.'

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/remove-whitespace.html"></iframe>