如何在javascript中的一定数量的字符后在空格处拆分字符串? [英] How do I split a string at a space after a certain number of characters in javascript?

查看:25
本文介绍了如何在javascript中的一定数量的字符后在空格处拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个很好的长字符串,我需要在 Javascript 中在一定数量的字符后面的空格处拆分它.例如,如果我有

你是狗,我是猫."

并且我希望它在 10 个字符后拆分,但在下一个空格处......所以我希望下一个空格成为拆分点,而不是拆分狗.

希望我写得很清楚,解释起来有点尴尬.

我需要将所有这些存储到一个数组中.所以按照我的描述拆分字符串,但将它存储到一个我可以迭代的数组中.抱歉造成混乱 - 就像我说的,描述起来有点奇怪.

解决方案

考虑:

str = "剃刀背跳跃的青蛙是如何将六名愤怒的体操运动员练级的!"result = str.replace(/.{10}S*s+/g, "$&@").split(/s+@/)

结果:

<预><代码>[多么锋利的跳跃",青蛙可以调平",六激",体操运动员!"]

So I have a nice long string that I need to split in Javascript at a space following a certain amount of characters. For instance, if I have

"You is a dog and I am a cat."

and I want it to split after 10 characters but at the next space... so rather than splitting dog up I want the next space to be the split point.

I hope I wrote that clearly, its a bit awkward to explain.

EDIT: I need to store all of this into an array. So splitting the string up as I described, but storing it into an array which I can iterate through. Sorry for the confusion- like I said, a bit odd to describe.

解决方案

Consider:

str = "How razorback-jumping frogs can level six piqued gymnasts!"
result = str.replace(/.{10}S*s+/g, "$&@").split(/s+@/)

Result:

[
 "How razorback-jumping",
 "frogs can level",
 "six piqued",
 "gymnasts!"
]

这篇关于如何在javascript中的一定数量的字符后在空格处拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆