this tool call regexr to help practice as well as learn new patterns. Check it out if you need help building and testing regex patterns.
Like most purpose-built tools, there is some learning curve to finding how to best use the interface provided. You really need to consider your match strategy and your replace strategy. Let's write some examples based on some real world examples I've come across.
Here we have a CSS example for a button component. This component can be grouped or have an icon inside it. The naming of the component in CSS is kinda redundant.
.button-group-component {
display: flex;
align-items: center;
justify-content: space-between;
}
.button-component {
color: white;
background-color: black;
display: inline-block;
padding: 1rem 2rem;
}
.button-component .button-icon-component svg {
fill: currentColor;
}
Let's remove the -component
part of the CSS declaration. We don't need it. So we are going to turn .button-group-component
into .button-group
and so on for the other 2 class declarations. All we need to do is find -component
and replace it with nothing/null/empty string.
fastmod -m --extensions css '(\-component)' ''
Running the above code on in our target folder will prompt us with this:
./example.css:1
- .button-group-component {
+ .button-group {
display: flex;
align-items: center;
Accept change (y = yes [default], n = no, e = edit, A = yes to all, E = yes+edit, q = quit)?
Here you can see all the options for the code that is going to be changed as well as a nice diff too. If we move from left to right in the options we get the following choices:
y
or enter
accepts the changes being shownn
will not change anything and will go to the next changee
will open your $EDITOR
without any changes appliedA
will accept this change and all of the future changes with reprompting youE
will open your $EDITOR
with any changes appliedq
will no accept the changes and stop the processIn this case we can hit enter
for each change. We will be prompted 4 times: once for each occurrence of the string -component
or we can skip all that and just hit A
and everything will be changed.
Recently I was working on a Vue project that had a lot of components in it. I wanted to start using webpack chunking in order to split the components up and have single chunks for each component instead of them being wrapped up together. In order to do that, I need to rewrite all my import
statements to use "dynamic imports" with a special magic comment that webpack picks up.
You can see the example below:
# old code: `import MyComponent from 'Components/MyComponent.vue';`
# new code: `const MyComponent = () => import(/* webpackChunkName: "MyComponent" */ 'Components/MyComponent.vue');`
# note: you may need to reorder your imports when using a dynamic import
fastmod -m -d ./ --extensions vue \
'import (.*?) from \'(.*?)\';' \
'const ${1} = () => import(/* webpackChunkName: "${1}" */ \'${2}\');'
Here you can see the snippet I wrote to do the refactor. The great thing about using fastmod
over find-and-replace, is that it allowed me to accept or deny the changes. I didn't want all the components updated this way. Especially imports that were not Vue components. So this approach was ideal.
Those are two decent examples but I have used this tool quite a lot for refactoring code. Here are some more examples of what I used it for:
isset
into array_get
on a Laravel (PHP) projectuse
statement in PHP to a single use
line{{ var }}
interpolation to v-text="var"
directives in a Vue projectAs you can probably already tell, the possibilities are endless! I have used this on database exports and even CSV files. It is a much nicer alternative to find-and-replace. Best of all? It's editor agnostic. So you can use it with any other tools.
I'm a full-stack developer, co-organizer of PHP Vancouver meetup, and winner of a Canadian Developer 30 under 30 award. I'm a huge Open Source advocate and contributor to a lot of projects in my community. When I am not sitting at a computer, I'm trying to perfect some other skill.