0

I write small scripting 'language' for my game.

I would like to allow for every JS string literal strings(`"').

I figured out how to check everything inside those using:

(?<e1>""|'|`)(?:\$\k<e1>|(?!\k<e1>).)*\k<e1>)

It works.
But now, I have a different trouble. I need to remove all tabs, that are not inside those types of quotes.

I looked up here how to match everything, that is not inside quotes:

\t(?=([^"\\]*(\\.|"([^"\\]*\\.)*[^"\\]*"))*[^"]*$)

And I got trouble connecting those two worlds so that "a`\t`" does not remove this middle tab as

\t(?=([^"'`$]*(\$.|['`"]([^"'`$]*\$.)*[^"'`$]*["`']))*[^"`']*$)

does. I know, I have to check for the last not-escaped (with $ not \) quote, but how do I do that?

Emma
  • 27,428
  • 11
  • 44
  • 69
SkillGG
  • 647
  • 4
  • 18

1 Answers1

1

You could match what you don't want and keep what you want using a capturing group.

In this case you could wrap your first pattern in a capturing group and add an alternation using the pipe | after it to match 1+ times a tab.

In the replacement use the first capturing group:

((?<e1>""|'|`)(?:\$\k<e1>|(?!\k<e1>).)*\k<e1>)|\t+
^                                            ^^^^^    

See a regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Thanks. I didn't know how to start thinking about it, but now, when you showed me how I will know for the future. Very clever way! Thank you very much. – SkillGG Apr 30 '19 at 08:42