Do you have solution to substring text with HTML tags in Javascript?
For example:
var str = 'Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, consectetur adipiscing elit.'
html_substr(str, 20)
// return Lorem ipsum <a href="#">dolor <strong>si</strong></a>
html_substr(str, 30)
// return Lorem ipsum <a href="#">dolor <strong>sit</strong> amet</a>, co
Taking into consideration that parsing html with regex is a bad idea, here is a solution that does just that 🙂
EDIT: Just to be clear: This is not a valid solution, it was meant as an exercise that made very lenient assumptions about the input string, and as such should be taken with a grain of salt. Read the link above and see why parsing html with regex can never be done.
Example: http://jsfiddle.net/danmana/5mNNU/
Note: patrick dw’s solution may be safer regarding bad html, but I’m not sure how well it handles white spaces.