Extracting substring based on the occurrence of a character using javascript

时间:2017-06-15 10:24:44

标签: 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?

1 个答案:

答案 0 :(得分:2)

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]);
}