删除字符串中的空格
test_str = test_str.replace(/\ +/g, "");
删除字符串中的回车
test_str= test_str.replace(/[\r\n]/g, "");
删除字符串中的空格和回车
test_str = test_str.replace(/\ +/g, "").replace(/[\r\n]/g, "");
本文介绍了如何使用正则表达式在JavaScript中清除字符串中的空格、回车和换行符。通过`replace()`函数结合正则,可以有效地清理文本,这对于数据处理和字符串格式化至关重要。
删除字符串中的空格
test_str = test_str.replace(/\ +/g, "");
删除字符串中的回车
test_str= test_str.replace(/[\r\n]/g, "");
删除字符串中的空格和回车
test_str = test_str.replace(/\ +/g, "").replace(/[\r\n]/g, "");
887
565

被折叠的 条评论
为什么被折叠?
