How to replace Unicode characters in the following scenario using javascript?

How to replace Unicode characters in the following scenario using javascript? Using javascript I want to replace Unicode characters with a wrapper according to their style. If possible include a range of Unicode characters([a-z]) in regex for styles other than regular.

  • input = abc๐‘ข๐‘ฃ๐‘ค๐‘ฅ๐š๐›๐œ๐๐’‡๐’ˆ๐’‰๐’Š
  • expected ouput = <span class="regular>abc</span><i> ๐‘ข๐‘ฃ๐‘ค๐‘ฅ</i><b>๐š๐›๐œ๐</b><b><i>๐’‡๐’ˆ๐’‰๐’Š</i></b>

text = 'abc๐‘ข๐‘ฃ๐‘ค๐‘ฅ๐š๐›๐œ๐๐’‡๐’ˆ๐’‰๐’Š';
text = text.replace(/([a-z]+)/,'<span class="regular">$1</span>');
text = text.replace(/([๐‘Ž๐‘๐‘๐‘‘๐‘’๐‘“๐‘”โ„Ž๐‘–๐‘—๐‘˜๐‘™๐‘š๐‘›๐‘œ๐‘๐‘ž๐‘Ÿ๐‘ ๐‘ก๐‘ข๐‘ฃ๐‘ค๐‘ฅ๐‘ฆ๐‘ง]+)/,'<i>$1</i>');
text = text.replace(/([๐š๐›๐œ๐๐ž๐Ÿ๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ]+)/,'<b>$1</b>');
text = text.replace(/([๐’‚๐’ƒ๐’„๐’…๐’†๐’‡๐’ˆ๐’‰๐’Š๐’‹๐’Œ๐’๐’Ž๐’๐’๐’‘๐’’๐’“๐’”๐’•๐’–๐’—๐’˜๐’™๐’š๐’›]+)/,'<b><i>$1</i></b>');

Answer

I think it’s just a matter of finding the Unicode ranges you want to replace, and normalizing (decomposing) the string:

let text = 'abc๐‘ข๐‘ฃ๐‘ค๐‘ฅ๐š๐›๐œ๐๐’‡๐’ˆ๐’‰๐’Š';
text = text.replace(/[u{1d41a}-u{1d433}]+/gu, s => `<b>${s.normalize('NFKD')}</b>`);
text = text.replace(/[u{1d44e}-u{1d467}]+/gu, s => `<i>${s.normalize('NFKD')}</i>`);
text = text.replace(/[u{1d482}-u{1d49b}]+/gu, s => `<b><i>${s.normalize('NFKD')}</i></b>`);

console.log(text);

Leave a Reply

Your email address will not be published. Required fields are marked *