最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to replace ↵ to n - Stack Overflow

programmeradmin1浏览0评论

I want to replace the string ↵ to \n.

let test = 'te↵s↵t'
test = test.replace(/↵/g, '\n')
console.log(test)

I want to replace the string ↵ to \n.

let test = 'te↵s↵t'
test = test.replace(/↵/g, '\n')
console.log(test)

I tried to use replace using regex. I want to get result te\ns\nt

How can I replace string?

Share Improve this question edited Apr 3, 2019 at 2:47 yqlim 7,1183 gold badges20 silver badges44 bronze badges asked Apr 3, 2019 at 2:38 kkangilkkangil 1,7564 gold badges16 silver badges27 bronze badges 2
  • 2 What's wrong with test.replace(/↵/g, '\n')? – Patrick Roberts Commented Apr 3, 2019 at 2:41
  • a = test.replace(/↵/g, '\\n') – Ibra Commented Apr 3, 2019 at 2:43
Add a ment  | 

3 Answers 3

Reset to default 4

If you really want to get string te\ns\nt, try this code :

let test = 'te↵s↵t'
test = test.replace(/↵/g, '\\n')
console.log(test)

test.replace(/↵/g, '\n') or test.replace(/\u21b5/g, '\n') should both work. ↵ is an HTML escape; you do not have a HTML string.

As it stands, your expression is trying to match the literal string ↵.

To make it work, you can just replace the literal character , or if you want to specify an escape sequence, use the Unicode escape sequence for (\u21b5):

let test = 'te↵s↵t';
test = test.replace(/↵/g, '\n');

console.log(test);

let test = 'te↵s↵t';
test = test.replace(/\u21b5/g, '\n');

console.log(test);

If you want to replace with the literal \n instead of a newline, your replacement sequence needs to be \\n instead of \n.

发布评论

评论列表(0)

  1. 暂无评论