我有两个链接:
1: /aaa/bbbb/ccccc.htm
2: /xxx/yyy.htm
哪些正则表达式能够匹配第二个链接?
我尝试过:
^\/.*\/.*[^\/].*
但是,它与所有人都匹配。
答案 0 :(得分:2)
我猜想我们可能希望同时传递两个URL,在这种情况下,我们将以:
开始(\/[a-z]+)?(?:\.htm)?
然后,如果您愿意,我们可以添加更多边界。
如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。
jex.im可视化正则表达式:
const regex = /((\/[a-z]+)?(?:\.htm)?)/gm;
const str = `/aaa/bbbb/ccccc.htm
/xxx/yyy.htm`;
const subst = `Group #1: $1\nGroup #2: $2\n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
如果您只希望传递第二个URL并使第一个URL失败,则只需在表达式中添加一些边界,也许类似于this的方法会起作用:
^\/[a-z]+\/[a-z]+.htm$