问题描述
所以我有一个不错的长字符串,我需要在Javascript中的一定数量字符后的空格处分割它.例如,如果我有
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
你是狗,我是猫."
我希望它在10个字符之后但在下一个空格处拆分...因此,我不想将狗拆分掉,而是希望下一个空格作为拆分点.
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.
我需要将所有这些存储到一个数组中.因此,按照我的描述将字符串拆分开,但是将其存储到一个数组中,可以进行迭代.抱歉让我感到困惑,就像我说的那样,有点奇怪.
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.
推荐答案
考虑:
str = "How razorback-jumping frogs can level six piqued gymnasts!"
result = str.replace(/.{10}\S*\s+/g, "$&@").split(/\s+@/)
结果:
[
"How razorback-jumping",
"frogs can level",
"six piqued",
"gymnasts!"
]
这篇关于如何在javascript中一定数量的字符后的空格处分割字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!