Sublime Forum

How to get variables defined in RegExp?

#1

In Javascript, I can write like this:

const newStr = oldStr.replace(/([\d\.]+)(?=rem)/g, r => +r * 0.24);

But in ST3, how can I get that variable($1) and use it to do some calculations?

0 Likes

#2

What I could figure out is

  • Find -> Find All
  • Copy all selections to a new file
  • Append the *0.24 to all selections
  • Select lines (Ctrl+L on Windows) and execute an Emmet command to eval the math expression
{ "keys": ["what_ever_keybinging_you_like"], "args": {"action": "evaluate_math_expression"}, "command": "run_emmet_action", "context": [{"key": "emmet_action_enabled.evaluate_math_expression"}]},
  • Paste back calculated selections back to the original file

It would be good if someone has a better solution to this.


Is there a plugin which can do Python eval on selections with something like $1 as the placeholder?

0 Likes

#3

You could write one pretty easily.

0 Likes

#4

If you’re a licensed user and using a Dev build, you can find numbers with a Find operation. then use the new Arithmetic command to perform calculations on it:

The Evaluate package may also be of assistance. The built in way is easier though (where available).

5 Likes

#6

And finally, there is also InsertNums, which you would use with the pattern f|_*0.24.

2 Likes