javascript - 正则表达式无法检测所有 url

标签 javascript regex

我想用正则表达式检测所有这些网址:

/liga-femenina-1
/liga-femenina-1/equipos
/liga-femenina-1/resultados
/liga-femenina-1/estadisticas
/liga-femenina-1/buscador
/liga-femenina-2
/liga-femenina-2/equipos
/liga-femenina-2/resultados
/liga-femenina-2/estadisticas
/liga-femenina-2/buscador

我定义了这个正则表达式:

\/liga-femenina-(1|2)\/?\w*

此正则表达式仅检测第一个网址。

enter image description here

但是,如果我删除第一个 url,则正则表达式会正确检测下一个 url,而之前的 url 则不会这样做。

enter image description here

如何让我的正则表达式检测到所有网址?我做错了什么?

最佳答案

尝试使用标志g(全局):

/\/liga-femenina-(1|2)\/?\w*/g

g

global match; find all matches rather than stopping after the first match

请注意:如果您在正则表达式中使用开始和结束,则使用标志m(多行):

/^\/liga-femenina-(1|2)\/?\w*+$/gm

multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)

关于javascript - 正则表达式无法检测所有 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847438/

相关文章:

javascript - 在 JavaScript 中提取值作为占位符变量

javascript - 正则表达式和 Javascript - 双竖线

javascript - 为什么这个正则表达式在 Angular 上的匹配仅在 Safari 上失败?

c# - .NET 真的使用 NFA 作为正则表达式引擎吗?

html - Firefox 输入模式正则表达式范围

javascript - 如何检查html是否已更改?

javascript - 将 <div> 置于无侧边栏区域的中心

javascript - ng-repeat 内的 ng-model 值不起作用

javascript - HTML 在所有设备上响应

javascript - 如果字符串包含数值,则从字符串中删除整个单词