I want to cut this string into 50 characters where (after a space) it starts count开发者_JS百科ing up to 50 (excluding spaces), and if the section is over 50 it inserts a space. So far, I can cut it up, but I don't know how to specify "exclude spaces". Tried [^\s]
but no joy.
var str = " http://this.domain.com/fff/222/widget.css http://www.domain.com/myfolder/uploads/1/3/3/7/2332053/custom_themes/8787687678644/more_custom_themes/files/my-main_style.css?8763487634 http://cdn.domain.com/folder/images/thisfolder/common.css?9444"
str.replace(/\s(\w.{50})/g,' $1 ');
Try this:
var str = " http://this.domain.com/fff/222/widget.css http://www.domain.com/myfolder/uploads/1/3/3/7/2332053/custom_themes/8787687678644/more_custom_themes/files/my-main_style.css?8763487634 http://cdn.domain.com/folder/images/thisfolder/common.css?9444"
str = str.replace(/([^ ]{50})/g, "$1 ");
If your reason for doing this is to prevent a long string from breeaking out of its area in a browser window, you may want to try the CSS solution:
CSS has a word-wrap
property, which you can use to tell the browser to break long words even if they don't have a natural break-point.
#divwithlongword {
word-wrap: break-word;
}
Now if you have HTML as follows:
<div id='divwithlongword'>myextraordinarilyandexcessivelylongdomainnamegoeshere.com</div>
...it will wordwrap where it needs to within the domain name.
This is supported in all major browsers -- see http://caniuse.com/#search=wordwrap
Hope that helps.
You use the following regex:
(\S{50})
\S - Matches any character that is not a whitespace character (spaces, tabs, line breaks).
精彩评论