Hi,
I`m back with some news.
apparently my initial goal was rather easy to accomplish. By playing with your code I realized I can get it with just one line.
This code will insert </wbr> tag to break words longer then 10 characters. It won`t count spaces and line breaks because the browsers deals with them automatically.
- var str = 'ThisStringIsTooLong and some normal words'
-
str.replace([/S]{10},'$&</wbr> ';
-
Then I faced another problem. In my application I`m replacing links with <a> tags. If the link text is longer then 10 characters I`ll break it. So I need a regex that will break the text of the link but not the address inside the <a>. For example:
- <a href="http://www.example.com">http://www<wbr/>.example.c<wbr/>om<a/>
The next code will do it:
- str = str.replace(/(?:<[^>]+>)|(.{10})/g,'$&<wbr/>');
-
str = str.replace(/><wbr\/>/,'>');
-
The second line will remove unwanted <wbr/> that will appear after each html tag.
It`s important to note that all the < and > characters in the normal text are escaped in a previous stage.
Thanks for you help guys.