Regex 仅从 href 获取 docx 或 doc 值

Regex Get only docx or doc value from href

在 Whosebug 中搜索后,我找到了这个正则表达式模式:

/href=['"]([^'"]+?)['"]/

它获取所有 href 的值。

现在我需要将该模式限制为仅获取 doc 或 docx 值。

请注意,link 可能会在 .docx.doc 之后添加。

例如,如果我有 link:

<a href="/site/file1.doc?id=1">link1</a>

结果应该是:

/site/file1.doc
/href=['"]([^'"]+?\.docx?)[^'"]['"]/

在这里查看:https://regex101.com/r/oS1cD0/2

试试这个:

/href=(['"])([^'"]+\.docx?(\?[^'"]*)?)/

这要求“.doc”或“.docx”之后的内容是 href 的末尾,一个问号后跟内容,即它不会' t 匹配 "foo.doctor".

这也确保引号通过反向引用在每一端匹配。

参见live demo