Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs Regex删除空格后的每一个新行字符_Javascript_Node.js_Regex - Fatal编程技术网

Javascript nodejs Regex删除空格后的每一个新行字符

Javascript nodejs Regex删除空格后的每一个新行字符,javascript,node.js,regex,Javascript,Node.js,Regex,给定如下字符串: "hello\n \n my dog is red\n \n and\n stripey\n \n \n with spots\n" 我想要这个: "hello\n my dog is red\n and\n stripey\

给定如下字符串:

"hello\n          \n            my dog is red\n          \n          and\n          stripey\n          \n            \n            with spots\n"
我想要这个:

"hello\n                      my dog is red\n                    and\n          stripey\n                                  with spots\n"

使用此正则表达式替换

/^[ \t]*\n/mg
使用空字符串,其中
^[\t]*
匹配从行首到
\n
的一个或多个空格或制表符,并将其删除

检查这个JS演示

var s='hello\n\n我的狗是红色的\n\n和带斑点的条纹\n\n\n';
console.log(“在:+s之前”)

console.log(“在:“+s.replace(/^[\t]*\n/mg,”)
您还可以使用
\s
而不是
[\t]
来匹配任何空格字符,而不仅仅是空格和空格tabs@Linschlager:是的,
\s
也可以。