用于在 HTML 中查找类名的正则表达式 [英] Regular expression for finding class names in HTML

查看:18
本文介绍了用于在 HTML 中查找类名的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 grep 来找出在一堆文件中是否/在哪里使用了 html 类.正则表达式模式不仅应该找到 <p class="foo">,还应该找到 <p class="foo bar foo-bar">.

I'd like to use grep to find out if/where an html class is used across a bunch of files. The regex pattern should find not only <p class="foo"> but also <p class="foo bar foo-bar">.

到目前为止,我可以在下面的示例中找到 class="foo",但无法使其适用于多个类名:

So far I'm able to find class="foo" with this example below, can't make it work with multiple classnames:

grep -Ern "class="result+("| )" *

有什么建议吗?谢谢!迈克

Any suggestions? Thanks! Mike

推荐答案

这样的事情怎么样:

grep -Erno 'class[ 	]*=[ 	]*"[^"]+"' *

这也将允许更多的空白,并应该给你类似的输出:

That will also allow for more whitespace and should give you output similar to:

1:class="foo bar baz"
3:class = "haha"

要查看使用的所有类,您可以将上面的输出通过管道传输到以下内容中:

To see all classes used, you can pipe output from the above into the following:

cut -f2 -d'"' | xargs | sort | uniq

这篇关于用于在 HTML 中查找类名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆