问题
I want to find all instances of a class named "validation" in all of my html files project wide. It's a very large project and a search for the word "validation" gives me hundreds of irrelevant results (js functions, css, js/css minified, other classes, functions and html page content containing the word validation, etc). It can sometimes be the second, third, or fourth class declared so searching for "class='validation" doesn't work.
Is there a way to specify that I only want results where validation is a class declared on an html block?
回答1:
Yes. In the sublime menu go to Find
--> Find in Files...
Then match what is in the following image.
回答2:
The first thing you will want to do is consider other possibilities with how you can solve this problem. Currently, it sounds like you are only using sublime text. Have you considered trying to use a command-line tool like grep
?
Here is an example of how it could be used.
I have a project called enfold-child
with a bunch of frontend assets for a wordpress project. Let's say, I want to find all of my scss
files with the class "home" listed in them somewhere, but I do NOT want to pull in built css
files, or anything in my node_modules
folder. The way i would do that is as follows:
Folder structure:
..
|build
|scss_files
|node_modules
|css_files
|style.css
grep -rnw build --exclude=*{.css} --exclude-dir=node_modules -e home
grep
= handy search utility.
-r
= recursive search.
-n
= provide line numbers for each match
-w
= Select only those lines containing matches that form whole words.
-e
= match against a regular expression.
home
= the expression I want to search for.
In general, the command line has most anything one could want/need to do most of the nifty operations offered by most text-editors -- such as Sublime. Becoming familiar with the command line will save you a bunch of time and headaches in the future.
回答3:
In SublimeText, right-click on the folder you want to start the search from and click on Find in Folder
. Make sure regex search is enabled (the .*
button in the search panel) and use this regex as the search string:
class="([^"]+ )?validation[ "]
That regex will handle cases where "validation" is the only classname as well as cases where its one of several classnames (in which case it can be anywhere in the list).
If you didn't stick to double quotes, this version will work with single or double quotes:
class=['"]([^'"]+ )?validation[ '"]
If you want to use these regexes from the command line with grep
, you'll need to include a -E
argument for "extended regular expressions".
来源:https://stackoverflow.com/questions/46104951/sublime-text-find-all-instances-of-an-html-class-name-project-wide