How do you split a slash in javascript?

You can also repeatedly do replace(), split(), and join() on the string to get the desired result

let str = 'var1/var2/var3';

str = str.split('').reverse().join('').split(/\/(.+)/).map(x => x.split('').reverse().join('')).reverse().filter(x => x);

console.log(str);

Explanation:

str
  .split('') /* ['v', 'a', 'r', '/', 'v', 'a', ...] */
  .reverse() /* ['3', 'r', 'a', 'v', '/', 'r', ...] */
  .join('') /* '3rav/2rav/1rav' */
  .split(/\/(.+)/) /* ['3rav', '2rav/1rav', ''] */
  .map(x => x.split('').reverse().join('')) /* ['var3', 'var1/var2', ''] */
  .reverse() /* ['', 'var1/var2', 'var3'] */
  .filter(x => x) /* ['var/var2', 'var3'] */


To split a URL, use the split() method. Use toString() before that. Let us see an example

Example

var newURL="http://www.example.com/index.html/homePage/aboutus/";
console.log(newURL);
var splitURL=newURL.toString().split("/");
console.log(splitURL);

Above, we have set forward slash in the split() function, since we need to split the URL after every such slash.

To run the above program, you need to use the following command −

node fileName.js.

Output

Here, my file name is demo150.js. This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo150.js
http://www.example.com/index.html/homePage/aboutus/[
   'http:',
   '',
   'www.example.com',
   'index.html',
   'homePage',
   'aboutus',
   ''
]

How do you split a slash in javascript?

Updated on 11-Sep-2020 08:36:01

  • Related Questions & Answers
  • How to replace before first forward slash - JavaScript?
  • Possible to split a string with separator after every word in JavaScript
  • Moving every alphabet forward by 10 places in JavaScript
  • Select text after last slash in MySQL?
  • Squaring every digit of a number using split() in JavaScript
  • Why should we use a semicolon after every function in JavaScript?
  • JavaScript Insert space after every two letters in string?
  • Do I need to use a semicolon after every function in JavaScript?
  • JavaScript - Redirect a URL
  • JavaScript example for Capturing mouse positions after every given interval
  • MySQL query to split a column after specific characters?
  • Kth smallest element after every insertion in C++
  • Replace() with Split() in JavaScript to append 0 if number after comma is a single digit
  • Java regex program to split a string at every space and punctuation.
  • Writing a custom URL shortener function in JavaScript

Examples

Split the words:

let text = "How are you doing today?";
const myArray = text.split(" ");

Try it Yourself »

Split the words, and return the second word:

let text = "How are you doing today?";
const myArray = text.split(" ");
let word = myArray[1];

Try it Yourself »

Split the characters, including spaces:

const myArray = text.split("");

Try it Yourself »

Use the limit parameter:

const myArray = text.split(" ", 3);

Try it Yourself »

More examples below.


Definition and Usage

The split() method splits a string into an array of substrings.

The split() method returns the new array.

The split() method does not change the original string.

If (" ") is used as separator, the string is split between words.


Syntax

string.split(separator, limit)

Parameters

Parameter Description
separator Optional.
A string or regular expression to use for splitting.
If omitted, an array with the original string is returned.
limit Optional.
An integer that limits the number of splits.
Items after the limit are excluded.

Return Value

Type Description
Array An array containing the splitted values.


More Examples

Split a string into characters and return the second character:

const myArray = text.split("");

Try it Yourself »

Use a letter as a separator:

const myArray = text.split("o");

Try it Yourself »

If the separator parameter is omitted, an array with the original string is returned:

const myArray = text.split();

Try it Yourself »


Browser Support

split() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes


How do you split a slash?

split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') . The str. split method will split the string on each occurrence of a forward slash and will return a list containing the results.

How do you split a symbol in JavaScript?

JavaScript Demo: Symbol.split.
class Split1 {.
constructor(value) {.
this. value = value;.
[Symbol. split](string) {.
const index = string. indexOf(this. value);.
return `${this. value}${string. substr(0, index)}/${string. substr(index + this. value. length)}`;.

How do you split an object in JavaScript?

JavaScript split() Method: String Object The split() method is used to split a string object into an array of strings by breaking the string into substrings. separator: The character to separate the string. The separator itself is a string. If the separator is not present it returns the entire string.

What is a separator in JavaScript?

separator: It is optional parameter. It defines the character or the regular expression to use for breaking the string. If not used, the same string is returned (single item array). limit: It is optional parameter.