正则表达式换行符仅替换为字符 n

问题描述 投票:0回答:1

我有这个javascript正则表达式:

let output = 'This role involves:\n   - Steering the direction of universal expansion and resource allocation.';
console.log('before:', JSON.stringify(output));

output = output.replace(/[^a-zA-Z0-9 _-\s\r\n\t]/gi, '');
console.log('after:', JSON.stringify(output));
// This role involvesn   - Steering the direction of universal expansion and resource allocation

它不会留在那里

\n
,而是用
n

代替它

其次,我想将

   - Steering
替换为一个空格,然后是减号,然后是另一个空格,然后是单词
 - Steering
。但似乎这不仅仅是一个普通的空间。或者我如何调试最好?

javascript regex
1个回答
0
投票

let input = `This role involves:
  - Steering the direction of universal expansion and resource allocation.`;
  

let output = input.replace(/\n/g, 'n').replace(/\\s+/g, '');
console.log(output);

© www.soinside.com 2019 - 2024. All rights reserved.