Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  10] [ 5]  / answers: 1 / hits: 18861  / 11 Years ago, fri, april 26, 2013, 12:00:00

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.


More From » jquery

 Answers
50

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!
]

[#78588] Thursday, April 25, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mikael

Total Points: 73
Total Questions: 115
Total Answers: 86

Location: Central African Republic
Member since Mon, Aug 10, 2020
4 Years ago
;