Splitting String and taking out specific elements in node js

javascript split regex
javascript split string at index
javascript split string by comma
javascript string slice
jquery split string to array
js substring
javascript truncate string
split is not a function

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.

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

Node.js, The split() function is a string function of Node. js which is used to split string into sub-strings. This function returns the output in array form. Parameters: This function accepts single parameter separator which holds the character to split the string. separator. Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item) limit. Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array.

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, ''));

String.prototype.split(), The split() method divides a String into an ordered set of substrings, puts these substrings into an array, and returns the array. the number of elements in the array, and the individual array elements. Removing spaces from a string Android, Opera for Android, Safari on iOS, Samsung Internet, Node.js  JavaScript has a very useful method for splitting a string by a character and creating a new array out of the sections. We will use the split() method to separate the array by a whitespace character, represented by " ".

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.

JavaScript String split() Method, The split() method is used to split a string into an array of substrings, and returns the new array. Tip: If an empty string ("") is used as the separator, the string is  You need to add to array2 and use both elements from the returned array that String.prototype.split returns - i.e. 0 is the left hand side and 1 is the right hand side of the dot.

Index, Slice, Split, and Manipulate JavaScript Strings, How To Index, Split, and Manipulate Strings in JavaScript If an empty parameter is given, split() will create a comma-separated array with each is a simple way to perform the common task of removing excess whitespace. The following example defines a function that splits a string into an array of strings using the specified separator. After splitting the string, the function logs messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements.

Convert String to Array with JavaScript's String split Method, JavaScript's string split method returns an array of substrings obtained by splitting a Invoke the split method on the string you want to split into array elements. Look for the given string to split by in the given string. If not found, return one item array with the whole string. If found, iterate over the given string taking the characters between each two occurrences of the string to split by. In case the given string starts with the string to split by, the first item of the result array will be empty.

[ JavaScript ] Splice, Slice, Split - Jean Pan, Splice, Slice, Split … they all start with S … Splice and Slice are almost twin of an array by removing existing elements or adding new elements using a specified separator string to determine where to make each split. JavaScript's string split method returns an array of substrings obtained by splitting a string on a separator you specify. The separator can be a string or regular expression. Invoke the split method on the string you want to split into array elements.

Comments
  • Please post the code you tried.
  • Did you try "Organisation/Guest/images/guestImage.jpg".split('/') ?
  • It works for me.
  • 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.
  • 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.