I have a string called completionBar
which contains this:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
I'm trying to replace a single ⬛ with ⬜, so I tried:
completionBar.replace(/\U+2B1B/, 'U+2B1C');
but nothing happen, what I did wrong?
I have a string called completionBar
which contains this:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
I'm trying to replace a single ⬛ with ⬜, so I tried:
completionBar.replace(/\U+2B1B/, 'U+2B1C');
but nothing happen, what I did wrong?
You can use /⬛/g
in .replace()
.
I would try as the following if you want to replace all:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
const result = completionBar.replace(/⬛/g, '⬜');
console.log(result)
If you need only the first to replace:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
const result = completionBar.replace('⬛', '⬜');
console.log(result)
I hope this helps!
In my opinion, you can use escape
and unescape
function to show exactly the string code. It easy to debug and maintain the code.
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
let escapeCompletionBar = escape(completionBar).replace(/u2B1B/g, 'u2B1C');
let result = unescape(escapeCompletionBar);