简体   繁体   中英

Extract variable but not function from JavaScript by Regex

I am trying to extract variables (with dot notation) but not functions from a string of JavaScript using Regex. This is what I have done so far.

var regex = /([_a-zA-Z0-9\[\]]+(?:\.)?)+/g;

However, this regex will also extract JavaScript functions. For example,

var regex = /([_a-zA-Z0-9\[\]]+(?:\.)?)+/g;
console.log("a.b.c(d.e) === f".match(regex));

will output

["a.b.c", "d.e", "f"]

but I am expecting

["d.e", "f"]

After the end of the match, you can negative lookahead for ( , to ensure that the captured substring is not a function call. Also negative lookahead for any of the characters in the same initial character set to make sure that the backtracking from a failed function match doesn't match part of the function substring:

 var regex = /([\\w[\\]]+\\.?)+(?![.\\w[\\]]|\\s*\\()/g; console.log("abc(de) === f + q[w]e && obj[prop]()".match(regex)); 

It would probably be better to repeat groups of . followed by word characters, or [ followed by word characters and ] :

 var regex = /\\w+(?:\\.\\w+|\\[\\w+\\])+(?![([\\].])/g; console.log("abc(de) === f + q[w].e && obj[prop]()".match(regex)); 

\\w+(?:\\.\\w+|\\[\\w+\\])+(?![([\\].]) means:

  • \\w+ - Word characters ( [A-Za-z0-9_]+ )
  • (?:\\.\\w+|\\[\\w+\\])+ - Repeat either:
    • \\.\\w+ - A . followed by word characters, or
    • \\[\\w+\\] - [ , followed by word characters, followed by ]
  • (?![([\\].]) - At the end of the pattern, negative lookahead for ( , [ , ] , and .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM