简体   繁体   English

在javascript中,使用保留顺序拆分字符串

[英]In javascript, spliting a string with order preserving

hosts=".uk.com:hostname:@10.10.10.10/10:@[2001:db8:1/64]:@11.11.11.11/11:@[::2/24]"

In javascript, how do i split the above string("hosts") string like the following : 在javascript中,如何分割上述string(“ hosts”)字符串,如下所示:

newhosts=.uk.com,hostname,@10.10.10.10/10,@[2001:db8:1/64],@11.11.11.11/11,@[::2/24]"

tried this : 尝试了这个:

var hosts, newhosts;
var ip6_hosts = [];
var ip6_re = /@\[(.*?)\]/g;

hosts=".uk.com:hostname:@10.10.10.10/10:@[2001:db8:1/64]:@11.11.11.11/11:@[::2/24]";
while ((match=ip6_re.exec(hosts)) != null)
    ip6_hosts.push(match[0]);
non_ip6_hosts=hosts.replace(ip6_re, '').replace(/:+/g, ':');
newhosts=ip6_hosts.concat(non_ip6_hosts.split(':'));

actual output : 实际输出:

newhosts=@[2001:db8:1/64],@[::2/24],.uk.com,hostname,@10.10.10.10/10,@11.11.11.11/11

expected output : 预期产量:

newhosts=.uk.com,hostname,@10.10.10.10/10,@[2001:db8:1/64],@11.11.11.11/11,@[::2/24]

but not sure how to preserve the order. 但不确定如何保留订单。 is there any way to achieve an expected output ? 有什么办法可以达到预期的输出?

Can't You just say: 你不能说:

host = host.replace(/:+/, ',');

whenever you want to change it? 每当你想改变它? I feel like this is too simple of an answer, comment if I'm not getting it. 我觉得这个答案太简单了,如果我不明白,请发表评论。

You could try: 您可以尝试:

    var openbracket=0;
    for (i=0; i<hosts.length; i++)
    {
    if (hosts.substr(i,1) == '[') openbracket=openbracket+1;
    if (hosts.substr(i,1) == ']') openbracket=openbracket-1;
            if ((hosts.substr(i,1) == ':') && openbracket==0)
            {
            hosts = hosts.substr(0,i) + ',' + hosts.substr(i+1,hosts.length-i-1);
            }
    }

seems to work for me, though I'm not sure if there's a better method for changing the value of hosts. 似乎对我有用,尽管我不确定是否有更好的方法来更改主机的价值。 All it needs to do is insert the ',' at the location i. 它需要做的就是在位置i处插入“,”。 The above code adds everything to the left of the ':', a ',', and everything to the right of the ':'. 上面的代码将所有内容添加到':'的左侧,一个',',并将所有内容添加到':'的右侧。

note: this assumes you don't want any ':' inside of brackets changed to a comma. 注意:这是假设您不希望将方括号内的任何“:”更改为逗号。
hope this helps. 希望这可以帮助。

以下应该工作:

hosts.replace(/([^:]{1})\:{1}([^:]{1})/g, '$1,$2')

Can you try this... 你可以试试这个吗...

String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);

} }

hosts=".uk.com:hostname:@10.10.10.10/10:@[2001:db8:1/64]:@11.11.11.11/11:@[::2/24]" hosts =“。uk.com:hostname:@10.10.10.10/10:@[2001:db8:1/64]:@11.11.11.11/11:@[::2/24]”

hosts = hosts.split(':@').join(',@'); hosts = hosts.split(':@')。join(',@');

var re = /:\\w/g; var re = /:\\ w / g; var found = hosts.match(re); var found = hosts.match(re);

hosts.replaceAt(found.index,','); hosts.replaceAt(found.index,',');

Try this. 尝试这个。

 var hosts='.uk.com:hostname:@10.10.10.10/10:@[2001:db8:1/64]:@11.11.11.11/11:@[::2/24]'; hosts = hosts.replace(/:@/g, ':@@'); hosts = hosts.split(':@'); var hostDetails = hosts[0].split(':'); var newHost = hostDetails.concat(hosts.splice(1, hosts.length)); console.log(newHost); 

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

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