1

I have Sample String like this

"Organisation/Guest/images/guestImage.jpg"

I need to take out Organisation,Guest separately. I have tried split() but can't get desired output.

4
  • Please post the code you tried. Commented Oct 24, 2018 at 12:26
  • Did you try "Organisation/Guest/images/guestImage.jpg".split('/') ? Commented Oct 24, 2018 at 12:29
  • It works for me. Commented Oct 24, 2018 at 12:35
  • Using "Organisation/Guest/images/guestImage.jpg".split('/') I am getting [ 'Organisation', 'Guest', 'images', 'guestImage.jpg' ] in which I can easily get the elements of Array. Thanks Reyon. Commented Oct 24, 2018 at 12:37

3 Answers 3

2

var str = "Organisation/Guest/images/guestImage.jpg";
var res = str.split("/");
    
console.log(res[0]);
console.log(res[1]);

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

Comments

1

You can use of String.replace() along with regex

const regex = /Organisation\/|\/Organisation/;

console.log('Organisation/Guest/images/guestImage.jpg'.replace(regex, ''));

console.log('Guest/Organisation/images/guestImage.jpg'.replace(regex, ''));

console.log('Guest/images/guestImage.jpg/Organisation'.replace(regex, ''));

Comments

0
var yourString = "Organisation/Guest/images/guestImage.jpg";

yourString.split('/')
// this returns all the words in an array

yourString[0] // returns Organisation
yourString[1] // returns Guest and so on

When you run .split() on a string, it will return a new array with all the words in it. In the code I am splitting by the slash /

Then I save the new array in a variable. Now you should know we can access array properties like this: array[0] where 0 is the first index position or the first word, and so on.

2 Comments

Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
Thank you for the heads up, I'll do.

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.