0

So, I have a string which is actually a javascript script. I need to remove first reserved javascript word from it, but only if it actually has the meaning of the reserved word. That means:

it can't be inside string literals ("" or '', like "return that thing to me");
it has to be preceded and followed by whitespace, linebreak and such;
any other cases where it's not a reserved word.

I have the hard time trying to write RegExp for this, as there always seems to be at least one case it doesn't work as intended.

Any help, please?

isemsez
  • 1
  • 1
  • 3
    *"I have hard time trying to write RegExp for this..."* That's because a single regex can't handle the complexities of a non-regular language. This is as true of JavaScript as it famously is [of HTML](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#1732454). You need a *parser*, like Esprima, instead. – T.J. Crowder Jan 09 '18 at 08:12

1 Answers1

0

You have to use a more powerful method than regex - such as syntax analyzer to break your string into an abstract syntax tree. Then look for any keyword you want.

Try using the parser API of the Spider Money.

Or a library like UglifyJS or Esprima

Charlie
  • 22,886
  • 11
  • 59
  • 90