避免在单词中间分割字符串

时间:2017-11-29 08:02:11

标签: jquery

我在下面的代码中提到了以下代码。如果字符串长度超过X个字符,我试图拆分字符串。

问题在于它有时会将字符串拆分为单词的中间部分。

像这样:

pursue pleasure r", <-- splitted here
  "ationally encounter

在单词之前或之后分割字符串是否可行。所以它不会破坏任何一个词?

&#13;
&#13;
var str = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?";

var blocks;
if(str.length > 400){
  blocks = str.match(/.{1,400}/g)
}
console.log(blocks)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用\S to match any negated whitespace character class(即匹配到下一个空格)。

var str = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?";

var blocks;
if(str.length > 400){
  blocks = str.match(/.{1,400}\S*/g)
}
console.log(blocks)

或者匹配到下一个word boundary(\b) non-greedy repeat(最小匹配)。

var str = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally. encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?";

var blocks;
if(str.length > 400){
  blocks = str.match(/.{1,400}.*?\b/g)
  // in case you need `.` as well then use following
  // regex - /.{1,400}.*?\b\.?/g
}
console.log(blocks)