如何使用一个衬线从字符串中提取特定子字符串

问题描述 投票:0回答:2

输入:parent/123/child/grand-child

预期输出:child

尝试1(?<=\/parent\/\d*)(.*)(?=\/.*)

错误:lookbehind内部的量词使它的宽度不固定,look back不接受*,但我不知道该数字的宽度,因此必须使用它]]

尝试2:

(有效但有2个衬里):
const currentRoute='/parent/123/child/grand-child'
let extract = currentRoute.replace(/\/parent\/\d*/g, '');
extract = extract.substring(1, extract.lastIndexOf('/'));
console.log('Result', extract)  

如何使用一个衬管(最好使用正则表达式)获得提取物>>

输入:parent / 123 / child / grand-child预期输出:child尝试1:(?<= \ / parent \ / \ d *)(。*)(?= \ /.*)错误:内部有一个量词后向使其不固定宽度,后向不...

javascript regex
2个回答
0
投票

怎么样

currentRoute.match(/\/parent\/(?:.*)\/(.*)\//)[1]

-1
投票

© www.soinside.com 2019 - 2024. All rights reserved.