0

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

10
  • Tried anything ? Commented Jun 29, 2018 at 17:32
  • 1
    "I have following string". No you don't. You can't have that code in any javascript anywhere without getting a syntax error. Commented Jun 29, 2018 at 17:34
  • Does your string really have all that whitespace? Commented Jun 29, 2018 at 17:35
  • is your string correct? having space between 2 urls? Commented Jun 29, 2018 at 17:36
  • 1
    for what you tried, don't you mean to start that line with myString? Commented Jun 29, 2018 at 17:48

6 Answers 6

4

You can trim() the string first, then split on whitespace. This saves you from having to loop over the results again 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+/))

Sign up to request clarification or add additional context in comments.

Comments

2

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(/\r\n|\n/).map(i => i.trim())

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

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

Comments

0

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 :

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)

Comments

0

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(/\r\n|\n/).filter(Boolean);

1 Comment

That's not what the OP's input string looks like and this won't work not the OP's string.
0

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))

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.