gpt4 book ai didi

javascript - 仅拆分和组合字符串的一部分

转载 作者:行者123 更新时间:2023-11-30 14:32:25 26 4
gpt4 key购买 nike

我有一个字符串,例如:https://192.168.22.34/www/index.html

我只想提取 IP 地址的最后两部分,即 2234 来制作端口号。

目前,我在做:

port = url.split('.').slice(2,4).join("").split('/').slice(0,1).join("");

有没有更简洁的方法来做到这一点?或者这是我能做的最好的?

最佳答案

你可以使用这个:

var url = 'https://192.168.22.34/www/index.html'
port = url.split('/')[2].split('.').slice(2,4).join('');

console.log(port);

也可以使用正则表达式做同样的事情:

var url = 'https://192.168.22.34/www/index.html'
port = url.match(/\d{1,3}/g).slice(2,4).join('');

console.log(port);

关于javascript - 仅拆分和组合字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50972040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com