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
3 Answers
Reset to default 4If 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
.