0

This task looks like easy but I can not solve it :(

Regex demo: https://regex101.com/r/qzIGlZ/1

Demo

As you can see, I want to copy abc3, but regex find first word var and copy all var till 3.

How to copy starting from nearest var to my key word 3?

Now it works like this: find var and copy all till 3

But I need it to work like this: find var, starts copy, but if find new var - start copy from that position.

p.s. I need solution not only for that example with "var name", I searching universal method to do "closest search starting with something and till something"

More difficult exapmles: #1 #2

p.p.s. This is NOT ONLY for JS.

mixalbl4
  • 3,507
  • 1
  • 30
  • 44
  • you can use `\S` to match non-whitespace characters [`var (\S*) = 3`](https://regex101.com/r/qzIGlZ/2) – Slai Nov 18 '17 at 22:16
  • @Slai for only "var name" example - yes but not for all other examples, I need something universal like in "logic" in post. – mixalbl4 Nov 18 '17 at 22:35
  • @WiktorStribiżew That is the case when I exactly developing "JS code parser" (js unpacker section) and I need that think in regex :( And my question sounds like regex question (not only for JS) because I have same situation in other tasks when I need to copy something BEFORE key, not AFTER. – mixalbl4 Nov 18 '17 at 23:00
  • @WiktorStribiżew p.s. If there is no way to do this or you do not know how to do it, this is not an excuse to put -1 and mark with a duplicate that does not solve the problem. And you saw it: `I need solution not only for that example with "var name", I searching universal method to do "closest search starting with something and till something"` ? – mixalbl4 Nov 18 '17 at 23:21
  • I changed it to the correct close reason, so still a duplicate. You may still rely on a tempered greedy token, as [shown here](https://regex101.com/r/uFQmwQ/2). – Wiktor Stribiżew Jul 23 '18 at 19:42

0 Answers0