Splitting strings with Javascript without using for loop


Splitting strings with Javascript without using for loop



I have following string:


var myString = " https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg "



ANd I would like to get the following array:


var myArray = ["https://image.ibb.co/fShWGd/samhily.jpg","https://image.ibb.co/iDbDUy/ramhily.jpg", "https://image.ibb.co/cvbf9y/devoly.jpg".



Any ideas on how I would do that?



Tried this:


myArray.split(/(s+)/).filter( function(e) { return e.trim().length > 0; } );



But I get t when I do console.log(myArray[1])


t


console.log(myArray[1])





Tried anything ?
– j08691
Jun 29 at 17:32





"I have following string". No you don't. You can't have that code in any javascript anywhere without getting a syntax error.
– gforce301
Jun 29 at 17:34





Does your string really have all that whitespace?
– Mark_M
Jun 29 at 17:35





is your string correct? having space between 2 urls?
– NullPointer
Jun 29 at 17:36






for what you tried, don't you mean to start that line with myString?
– Garrett Motzner
Jun 29 at 17:48


myString




7 Answers
7



You can trim() the string first, then split on whitespace. This saves you from having to loop over the results again map():


trim()


map()




var myString = ` https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg `

console.log(myString.trim().split(/s+/))



You want to use split to split on the new lines:




var myString = ` https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg `

var result = myString.split(/rn|n/).map(i => i.trim())

// Result of all items
console.log(result)

// Result of first item in the array
console.log(result[1])



you can use .match with a Regex to match the substrings starting with https and ending with jpg , this will match them even if they are in a single line :


https


jpg




var myString = `https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg`

var result = myString.match(/https(.*)jpg/g)

console.log(result)



if your string having space in between urls then you can do like below:




var myString = " https://image.ibb.co/fShWGd/samhily.jpg https://image.ibb.co/iDbDUy/ramhily.jpg https://image.ibb.co/cvbf9y/devoly.jpg "

console.log(myString.trim().split(" ").map(i => i))



for case of whitespace try this:




var myString = ` https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg `

console.log(myString.trim().split(/rn|n/).map(value => value))



You can use trim split filter and map to achive it.


var myString = ` https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg `

myString.trim().split(/rn|n/).filter(Boolean);





That's not what the OP's input string looks like and this won't work not the OP's string.
– Mark_M
Jun 29 at 17:49






@Mark_M update my solution.
– anshu purohit
Jun 29 at 17:54



A simple reg exp to match anything by spaces would get what you want.




var myString = ` https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg `

console.log(myString.match(/[^s]+/g))



Assuming your string has all that whitespace (and therefore would need to be quoted with back-ticks (`)):


`


var myString = ` https://image.ibb.co/fShWGd/samhily.jpg
https://image.ibb.co/iDbDUy/ramhily.jpg
https://image.ibb.co/cvbf9y/devoly.jpg `

myString.trim().split(/s+/)



First, this gets rid or whitespace on the ends of myString, then it splits on any group of whitespace (s) characters.


myString


s






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV