Ashok.N
Ashok.N

Reputation: 1391

Extracting substring based on the occurrence of a character using javascript

I have the following string.

http://localhost:8080/test/tf/junk?tx=abc&xy=12345

Now I want to get substring between the third and fourth slash "/" using javascript.

To be more clear, I want to extract test from the above given string. Can anybody help me out on this?

Upvotes: 0

Views: 30

Answers (1)

Kashkain
Kashkain

Reputation: 523

You can split your string into an array

var str = "http://localhost:8080/test/tf/junk?tx=abc&xy=12345";
str = str.replace("http://", "");
var array = str.split("/");
for(var i = 0; i < array.length; i++) {
  alert(array[i]);
}

Upvotes: 2

Related Questions