Find soft hyphens in HTML document

1k views Asked by At

Let's look at following code:

$('#el').html('ex­am­ple');

Now, how can I get back that element's text with soft hyphens entities? Both of these:

$('#el').html();
$('#el').contents().get(0).nodeValue;

gives me "example" as return value, not "ex­am­ple"

jsFiddle link: http://jsfiddle.net/w7QKH/

Browser: FF7, not checked in other browsers.

2

There are 2 answers

1
welldan97 On BEST ANSWER

Actually $('#el').html() gives you 'example' with soft hyphens. If you run $('#el').html().length it will return 9. So hyphens are in, but they are not displayed. And it is not equal 'ex­am­ple' , because this string is not escaped. If you want to compare to string you should use 'ex\u00ADam\u00ADple'- here I replaced ­ with its unicode value. http://jsfiddle.net/w7QKH/1/

0
Alohci On

$('#el').html().replace(/\u00AD/g, '­');

See http://jsfiddle.net/K9mUy/