Reputation: 2632
I would like to replace all occurrences of a particular set of numeric values with their respective scalar multiples using the regex find/replace function in Atom or Sublime, preferably Atom.
For e.g., convert
headerContainer: {
fontSize:'1em',
padding: '1.5rem',
},
to
headerContainer: {
fontSize: 16,
padding: 24,
},
My regex find:
(')([\d.]+)r?em(')
My regex replace possibility:
$2 \* 16
Upvotes: 1
Views: 825
Reputation: 22791
As has been mentioned in the comments on your question, this is not directly possible with just a regular expression find and replace, but it is nonetheless possible in Sublime Text (and maybe Atom if it has similar functionality) using built in functionality.
To do this in Sublime, you want to use a combination of a Find All
to select all of the text and the built in Arithmetic
command to alter the selection. The specifics of how the Arithmetic
command works can be found in this answer, so I'm not going to cover that here.
To do what you want here, do the following:
Use the Find
panel (or the Replace
panel) to search for the regex '[\d.]+r?em'
, but click the Find All
button in the panel, which will select all of the matching items in the buffer.
Use the Arithmetic
command from the command palette and enter the expression round(float(x.strip("'rem"))*16)
and press enter to run the command.
The expression here modifies the selected text (x
) by removing the single quotes and the characters rem
, turns the resulting string into a floating point value, does the multiplication, and then rounds the value.
The result of that is something like this:
Upvotes: 8