js正则移除字符串多处空白和多类型换行符
2021-03-26
/**
* 过滤所有空格、回车符
*/
function filterEmpty(str) {
str = str.replace(/(\r\n|\n|\r)/gm, '');
str = str.replace(/\s+/g, '');
return str;
}
(详解)定义示例字符串:
var someText = "Here's some text.\n It has some line breaks that will be removed \r using Javascript.\r\n";
A:移除三种类型换行符
someText = someText.replace(/(\r\n|\n|\r)/gm,"");
B:多处空白换用单个空格
someText = someText.replace(/\s+/g," ");
C:移除所有空格
someText = someText.replace(/\s+/g, "");
参考:https://www.textfixer.com/tutorials/javascript-line-breaks.php