0

I have the following jQuery

$("h1").text($("h1").text().replace(/([^\s-]{5})(?=[^\s-])/g, '$1­'));

&shy; is supposed to be optional "-" + <br />, so I want it to be there in case someone enters a very long word as the title so that instead of being chopped off, it is split onto separate lines.

currently, it will change "ThisVeryLongWord" to "ThisV&shy;eryLo&shy;ngWor&shy;d" on the page where it should change it to

ThisV-
eryLo-
ngWor-
d

(assuming that the word needs to be broken at each spot)

what am I doing wrong?

Gianfranco P.
  • 8,605
  • 5
  • 46
  • 64
Alex
  • 1,032
  • 3
  • 13
  • 32
  • 1
    Possible duplicate of [JQuery: How to text insert HTML ascii character?](http://stackoverflow.com/questions/2726935/jquery-how-to-text-insert-html-ascii-character) – Gianfranco P. May 08 '17 at 15:51

2 Answers2

1

Try using html() instead of text(), at least for setting the value.

Julien Ch.
  • 1,231
  • 8
  • 15
1

Are you looking for this?

$("h1").html($("h1").text().replace(/([^\s-]{5})(?=[^\s-])/g, '$1&shy;'));​

Live demo

Engineer
  • 45,891
  • 11
  • 86
  • 90
  • maybe I wasn't clear enough, but ­ will do -
    only if it has to break the word. It will not always break it. see http://www.quirksmode.org/oddsandends/wbr.html
    – Alex Jul 10 '12 at 15:08
  • @Alex Ok, it is clear now. I haven't seen `­` before, and was not aware of it. – Engineer Jul 10 '12 at 15:11