Javascript split url by slash


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',
   ''
]

Javascript split url by slash

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

Ok lets say I have a URL

example.com/hello/world/20111020 (with or without the trailing slash). What I would like to do is strip from the url the domain example.com. and then break the hello world 20111020 into an array. But my other problem is. Sometimes the URL has no /hello/world/20111020 or just /hello/ so I need to first determine if there is anything after example.com if there not, then do nothing as obviously there's nothing to work with. However if there is something there for each / I need to add it to this array in order. So I can work with the array[0] and know it was hello.

I tried something a couple days back but was running into issues with trailing slashes it kept breaking the script, I unfortunately abandoned that idea. And today I am looking for fresh ideas.

asked Oct 20, 2011 at 18:18

This should work

var url = 'example.com/hello/world/20111020/';
//get rid of the trailing / before doing a simple split on /
var url_parts = url.replace(/\/\s*$/,'').split('/'); 
//since we do not need example.com
url_parts.shift(); 

Now url_parts will point to the array ["hello", "world", "20111020"].

answered Oct 20, 2011 at 19:20

Narendra YadalaNarendra Yadala

9,4641 gold badge25 silver badges43 bronze badges

2

You can use the jQuery-URL-Parser plugin:

var file = $.url.attr("file"); 

In your case you'd probably want to use segment():

var segments = $.url('http://allmarkedup.com/folder/dir/example/index.html').segment(); 

// segments = ['folder','dir','example','index.html']

answered Oct 20, 2011 at 18:28

Javascript split url by slash

James JohnsonJames Johnson

45k8 gold badges71 silver badges108 bronze badges

   <script type="text/javascript">
    function splitThePath(incomingUrl){
     var url = document.createElement("a");
     url.href = incomingUrl;
    //url.hash  Returns the anchor portion of a URL
    //url.host  Returns the hostname and port of a URL
    //url.hostname  Returns the hostname of a URL
    //url.href  Returns the entire URL
    //url.pathname  Returns the path name of a URL
    //url.port  Returns the port number the server uses for a URL
    //url.protocol  Returns the protocol of a URL
    //url.search    Returns the query portion of a URL
    if(url.pathname && url.pathname != ""){
   var pathnameArray = url.pathname.split("/");
 }else{

}


}
</script>

answered Oct 20, 2011 at 18:31

DefyGravityDefyGravity

5,4215 gold badges32 silver badges46 bronze badges

1

I have created the following regular expression for URLs

^https?://(((0|([1-9][0-9]{0,1}))(\.(0|([1-9][0-9]{0,1}))){3})|([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*(\.([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*))*))(/|((/([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*))*))$

It has been written for MySql - I am sure with a bit of fiddling you can get it you work for your needs.

BTW - I took the idea from an RFC - The number escapes me at this moment

answered Oct 20, 2011 at 18:32

Javascript split url by slash

Ed HealEd Heal

58.1k16 gold badges85 silver badges122 bronze badges

0

For parsing URLs, one different approach can be using anchor DOM object.

var a = document.createElement("A");
a.href = 'http://example.com:8080/path/to/resources?param1=val1&params2=val2#named-anchor';

a.protocol; // http:
a.host; // example.com:8080
a.hostname; //example.com
a.port; // 8080 (in case of port 80 empty string returns)
a.pathname; // /path/to/resources
a.hash; // #named-anchor
a.search // ?param1=val1&params2=val2

answered Nov 30, 2015 at 18:23

Shoaib NawazShoaib Nawaz

2,2224 gold badges29 silver badges37 bronze badges

How to split a url in JavaScript?

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

How do you split a URL in node?

You can split the url string using the split() method by specifying a delimiter ( / in your case) and it will return an array.

How do I find part of a URL?

window. location. pathname. split('/'); is a more elegant solution in most cases if you are trying to access the different sections of the URL beyod the standard protocol and www etc.

How do you get the string after last?

To get the value of a string after the last slash, call the substring() method, passing it the index, after the last index of a / character as a parameter. The substring method returns a new string, containing the specified part of the original string.