3 回答

TA贡献1757条经验 获得超8个赞
您可以使用否定字符类
^(?:[^\/]*\/){2}[^\/]*
const path = '/content/test-site/en_US/abc.html';
const desired = path.match(/^(?:[^\/]*\/){2}[^\/]*/)
console.log(desired[0])

TA贡献1810条经验 获得超4个赞
这是另一种方法:
(?:\/[a-zA-Z0-9-]+){2}
解释:
(?: # Non-capturing group
\/[a-zA-Z0-9-]+ # Match starting with / and followed with characters/digits/-
) # Close grouping
{2} # Match two times, i.e. only match with two /

TA贡献1836条经验 获得超13个赞
正则表达式匹配任何字符期望/:
content\/[^\/]+
这是字符类。以脱字符开头的字符类将匹配该类中没有的任何内容。更多关于这一点。
因此,使用 javascript:
const url = '/content/test-site/en_US/abc.html';
const path = url.match(/content\/[^\/]+/)
console.log(path[0])
添加回答
举报