我有这个字符串:https://[email protected]/happy.git
我想得到结果:https://github.com/happy.git
(第二个/
之后和@
之后没有随机字符串,但没有@
。
现在我有这样的东西:
var s = 'https://[email protected]/happy.git';
var d = s.substring(s.indexOf('/')+2, s.indexOf('@')+1;
s = s.replace(d, "");
它有效,但是我知道这是一个丑陋的解决方案。什么是最有效,最通用的解决方案?
尝试一下:
const indexOfAtSign: number = receivedMessage.indexOf('@')+1
const httpsString: string = 'https://'
const trimmedString: string = s.slice(indexOfAtSign)
const requiredURL: string = httpsString.concat(trimmedString)
// Print this value of requiredURL wherever you want.
因此,我的代码在这里执行,它获得@
的位置并删除其前面的所有内容以及符号本身。然后使用slice()
函数,剩下的就是我命名为trimmedString的其余部分。现在我已经预定义了https字符串,现在我们只需要合并它们即可。完成:-)
我已经在电报机器人中尝试过了,这是它的工作方式: