我想搜索并列出一个(或多个)HTML 页面中的所有图像 URL。使用 Regexr.com,我能够将这个正则表达式放在一起:src=".*(\.png|\.jpg|\.jpeg|\.gif)"
效果很好 在 regexr.com ,但不在命令行上。当我运行 grep -o 'src=".*(\.png|\.jpg)' index.html
时,上面的正则表达式得到零结果 这是 javascript 正则表达式和 grep 正则的区别吗我不知道的表达方式?
最佳答案
默认情况下,grep
使用较旧、功能较弱的正则表达式引擎。使用 egrep
变体将为您提供与 JavaScript 提供的非常相似的行为。
这个模式对我有用(调整你原来的 grep
):
egrep -o 'src=".*(\.png|\.jpg)' index.html
或者(更好),只需使用 -E
标志:
grep -E -o 'src=".*(\.png|\.jpg)' index.html
来自 another answer :
-E switches grep into a special mode so that the expression is evaluated as an ERE (Extended Regular Expression) as opposed to its normal pattern matching
-E, --extended-regexp Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX .)
关于javascript - Grep 获取图像 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48633997/