Javascript 按一定顺序拆分字符串。例如Text/0000/Text/Text

Javascript 按一定顺序拆分字符串。例如Text/0000/Text/Text,javascript,Javascript,我需要从具有以下结构的字符串中拆分字符串/或获取子字符串,以较容易的为准 字符串将来自window.location.pathname或window.location.href,看起来像text/numbers/text/text e、 ggoogle.com/first/page/0000-1234/other/page 所以期望的结果应该是:另一页 我想连接数字后面的任何内容并删除/ 根据需求更改进行编辑。数字之间有一个破折号如果您希望字符串不带“/”,请尝试下面的一个: let str =

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

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

e、 g
google.com/first/page/0000-1234/other/page

所以期望的结果应该是:
另一页

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


根据需求更改进行编辑。数字之间有一个破折号如果您希望字符串不带“/”,请尝试下面的一个:

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'


如果希望字符串不带“/”,请尝试下面的一个:

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'


据我所知,这可以奏效

const pathname=window.location.pathname
常量正则表达式=/\/(\d*)\//g
const whatYouNeed=路径名
.split(正则表达式)
.pop()
.replace(//\//g',)
console.log(需要什么)

据我所知,这可以达到目的

const pathname=window.location.pathname
常量正则表达式=/\/(\d*)\//g
const whatYouNeed=路径名
.split(正则表达式)
.pop()
.replace(//\//g',)
console.log(需要什么)

尝试以下操作以获取编号后的路径名:
const regex=/\/\d*\/(.*)/;
const str='google.com/first/page/12345678/other/page';
常量[,路径名]=str.match(regex);
const result=pathname.split('/').join('')

console.log({result})
尝试以下操作以获取编号后的路径名:
const regex=/\/\d*\/(.*)/;
const str='google.com/first/page/12345678/other/page';
常量[,路径名]=str.match(regex);
const result=pathname.split('/').join('')

log({result})
你尝试了什么?你尝试了什么?它成功了,但后来我意识到,数字之间有一个破折号,它不起作用。所以数字会像/0000-1234/你知道可以做什么吗?它起作用了,但后来我意识到,数字之间有一个破折号,它不起作用。所以数字会像/0000-1234/你知道可以做什么吗?我支持这个Answare是因为它使用简单的字符串搜索,而不是用于简单字符串解析的“昂贵”正则表达式引擎。我支持这个Answare是因为它使用简单字符串搜索,而不是用于简单字符串解析的“昂贵”正则表达式引擎。