Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Zero-width non-joiner (‌) with innerHTML

Tags:

javascript

I'm trying to get a ‌ with innerHTML

The output should be

This div contains a zero-width‌‌non-joiner, a non-breaking space & an ampersand

But the output is:

This div contains a zero-width‌non-joiner, a non-breaking space & an ampersand

How can I get the ‌?

alert(document.getElementsByTagName('div')[0].innerHTML)
<div>This div contains a zero-width&zwnj;non-joiner, a non-breaking&nbsp;space &amp; an ampersand</div>

Fiddle: https://jsfiddle.net/yst1Lanv/

like image 349
Xaver Avatar asked Feb 08 '18 14:02

Xaver


People also ask

How do you write a zero width non-Joiner?

The ZWNJ is encoded in Unicode as U+200C ZERO WIDTH NON-JOINER ( &zwnj;).

How do you write zero width joiner?

The character's code point is U+200D ZERO WIDTH JOINER ( &zwj;). In the InScript keyboard layout for Indian languages, it is typed by the key combination Ctrl+Shift+1. However, many layouts use the position of QWERTY's ']' key for this character.

What is \u200c character?

The \u200c character is ZERO WIDTH NON-JOINER.

How do you remove zero width space in Python?

Use the str. replace() method to remove zero width space characters from a string, e.g. result = my_str. replace('\u200c', '') .


2 Answers

You can search for it using its unicode \u200c. Then replace it with &zwnj; string.

alert(document.getElementsByTagName('div')[0].innerHTML.replace(/\u200c/g, '&zwnj;'))
<div>This div contains a zero-width&zwnj;non-joiner, a non-breaking&nbsp;space &amp; an ampersand</div>
like image 90
yqlim Avatar answered Oct 17 '22 22:10

yqlim


Your character is in the extracted (innerHTML) text, just not encoded as its HTML entity.

If you want you can replace the character with its entity:

alert(document.getElementsByTagName('div')[0].innerHTML.replace(/‌/g, '&zwnj;'));
<div>This div contains a zero-width&zwnj;non-joiner, a non-breaking&nbsp;space &amp; an ampersand</div>

Yong Quan posted some nicer code than me, if you want your app to be more maintainable use the unicode. My regex above is pretty confusing, this is easier to read:

.replace(/\u200c/g, '&zwnj;')
like image 1
theonlygusti Avatar answered Oct 17 '22 22:10

theonlygusti