gpt4 book ai didi

javascript - 按一定顺序分割字符串。例如文本/0000/文本/文本

转载 作者:行者123 更新时间:2023-12-01 00:02:47 27 4
gpt4 key购买 nike

我需要拆分字符串/或从具有以下结构的字符串中获取更容易的子字符串。

字符串将来自 window.location.pathnamewindow.location.href,看起来像 text/numbers/text/text

例如google.com/first/page/0000-1234/another/page

所以期望的结果应该是:anotherpage

我想在数字后面连接任何内容并删除/。

根据要求更改进行编辑。数字之间有一个破折号

最佳答案

如果您想要不带“/”的字符串,请尝试以下一个:

let str = 'google.com/first/page/12345678/another/page';
let outputResult = str.split('/').join('');
output will be: --> 'google.comfirstpage12345678anotherpage'

对于特定情况,即“另一页”,您可以尝试以下操作:

let str = 'google.com/first/page/12345678/another/page';
let op = str.split('/');
let result = op.splice(op.length - 2).join('');

result ==> 'anotherpage'

关于javascript - 按一定顺序分割字符串。例如文本/0000/文本/文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60606143/

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