我需要分割字符串/或从具有以下结构的字符串中获取子字符串中的任意一个。
字符串来自window.location.pathname
或window.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'