有人可以帮忙吗。我需要获取两个斜杠之间的字符,例如:
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/