简体   繁体   English

从字符jquery的第n次出现将字符串拆分为两个子字符串

[英]split string to two substrings from nth occerence of a charactor jquery

i have a string like this . 我有这样的字符串。

var url="http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11"

i want to get substrings 我想得到子串

http://localhost/elephanti2/chaink/stores/stores_ajax_page

and

5/b.BusinessName/asc/1/11

i want to split string from the 7 th slash and make the two sub-strings 我想从第7 th slash拆分字符串并制作两个子字符串

how to do this ??, 这个怎么做 ??,

i looked for split() 我寻找了split()

but in this case if i use it i have to con-cat the sub-strings and make what i want . 但是在这种情况下,如果我使用它,就必须对子字符串进行匹配并做出我想要的。 is there a easy way ?? 有没有简单的方法?

try this one: 试试这个:

var url="http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11";
var parts = url.split('/');

var p1 = parts.slice(0,6).join('/');
var p2 = parts.slice(7).join('/');
alert(p1);
alert(p2);

p1 should get the first part and p2 is the second part p1应该是第一部分,p2是第二部分

You can try this regex. 您可以尝试此正则表达式。 Generally if your url pattern always follow this structure, it will work. 通常,如果您的网址格式始终遵循此结构,则它将起作用。

        var pattern = /(\w+:\/\/(\w+\/){5})/i;
        var url = "http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11";
        var result = url.split(pattern);
        alert(result[1]);
        alert(result[3]);

Try this : 尝试这个 :

var str = 'http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11',
  delimiter = '/',
  start = 7,
  tokens = str.split(delimiter).slice(start),
  result = tokens.join(delimiter);

var match = str.match(/([^\/]*\/){5}/)[0];

Find this fiddle 找到这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM