-1

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?

norbitrial
  • 14,716
  • 7
  • 32
  • 59
sfarzoso
  • 1,356
  • 2
  • 24
  • 65

2 Answers2

0

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!

norbitrial
  • 14,716
  • 7
  • 32
  • 59
  • oh really nice! how can I replace just one emojy? – sfarzoso Jul 11 '20 at 09:53
  • 1
    @sfarzoso Just extended my answer with that. – norbitrial Jul 11 '20 at 09:55
  • thanks! just last question: it is possible replace the last emojy of the string? – sfarzoso Jul 11 '20 at 09:59
  • You can get the last index with `lastIndexOf()` then using the answer's solution from here: [How do I replace a character at a particular index in JavaScript?](https://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript). – norbitrial Jul 11 '20 at 10:15
0

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);

rian
  • 370
  • 1
  • 6