在字符串中的网址前面插入空格

我有带有网址的字符串。因此,当用户键入它时,我必须在url前面添加空间。

这是字符串:

Hello this my profilehttps://my_pforile and thishttps://my_profile2

我需要如下所示的字符串:

Hello this my profile https://my_pforile and this https://my_profile2

谁能帮我怎么做?


暮色呼如
浏览 240回答 2
2回答

幕布斯7119047

您可以使用String#replacemethod替换https://为空白前缀。let str = 'Hello this my profilehttps://my_pforile and thishttps://my_profile2';console.log(  str.replace(/https:\/\//g, ' $&'))// or with positive look-aheadconsole.log(  str.replace(/(?=https:\/\/)/g, ' '))

动漫人物

这也可以工作,您可以使用join进行字符串拆分let str="Hello this my profilehttps://my_pforile and thishttps://my_profile2"let newstring=str.split("profile").join("profile ");console.log(newstring)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript