gpt4 book ai didi

javascript获取斜杠之间的字符

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

有人可以帮忙吗。我需要获取两个斜杠之间的字符,例如:

Car/Saloon/827365/1728374

我需要获取第二个和第三个斜杠之间的字符。即 827365

最佳答案

您可以使用 split() String 的方法原型(prototype),将斜杠作为分隔符字符串传递:

const value = 'Car/Saloon/827365/1728374';
const parts = value.split('/');

// parts is now a string array, containing:
// [ "Car", "Saloon", "827365", "1728374" ]
// So you can get the requested part like this:

const interestingPart = parts[2];

也可以将其作为单线实现:

const interestingPart = value.split('/')[2];

文档在这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

关于javascript获取斜杠之间的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8519734/

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