How can i use regexp with .slice() in order to save my punctuation at the end of each line? Lets say i have a description which looks like this:
<div class="desc">We have text. About specific product? Or feature!</div>
I want it to look like this:
<div class="desc">
<ul>
<dd>We have text.</dd>
<dd>About specific product?</dd>
<dd>Or feature!</dd>
</ul>
for me jQuery is ok in this specific case, so i have build this:
jQuery(function() {
var description = jQuery('div.desc');
var sentences = description.text().split(/\.|\?|\!\s+/g),
$wrapper = description.empty();
jQuery.each(sentences, function(_, sentence) {
jQuery('<dd>', {text: sentence}).appendTo($wrapper);
});
jQuery('.desc > dd').wrapAll("<ul></ul>");
jQuery('.desc > ul > dd:last-child').remove();
});
and it works perfecly fine EXEPT -> slicing by "." "!" "?" - removes these signs. I need to keep them..
i also thought about something like:
var myStr = 'We have text. About specific product? Or feature!';
var strArray = myString.split('.');
after that i can use this (just for example):
alert('!' + strArray[1]);
BUT the thing is that description is always different. So if on one page "?" sign will be at the end of first sentence -> page number two will have it after sentence number 6.