简体   繁体   中英

jQuery Regex Search - Store All Variable Names

I am updating my jQuery syntax highlight script for ECMAScript 5 and adding improvements for JavaScript syntax highlighting. I would like to search through my code .html(); variable and store all variable names under a new variable varNames .

Variable Naming Examples:

Example 1).

var a = this;
var b = that;

Would add "a" and "b" to variable names list

Example 2).

var a = this,
    b = that;

Would add "a" and "b" to variable names list

Update: Example 3

parseXSL: r => F.parseXML(r).then(F.xsl),
xsl: x => {},
xslTransform: f => {},

Would add "r" "x" and "f" to the variable names list.

UPDATE 2: It appears that the above "Example 3" is a bit different whereas you could have the following:

 blob: r => r.blob(), arrayBuffer: r => r.arrayBuffer(), blobType: type=> r=> F.arrayBuffer(r).then(a=>new Blob([a],{type:type})), url: blob => 

Whereas I would give 'blob' the variable highlight anywhere after url: and not before so 'blob' would not be highlighted in r.blob(),

If this is another ballpark, please let me know and I'll remove it from here, play around with any answers to see if I can get this working too and if not post another question!


UPDATE 3: It appears what I said above is consistent with all variable names! They should only be highlighted after they have been defined as a variable name!

Any variable name and "THIS" ("example: THIS") should be wrapped in <spanvar></spanvar> purposely to be replaced with <span class="var"></span> later in my script as otherwise some bugs will occur.

Searching For A Variable Name:

For Example 1). , the much easier variable name to obtain, /(var )([a-zA-Z0-9 _]+)/ can be used, however I've not been working with regexes for long and the variable value could be anything which includes multiple commas, so I'm not sure how to find for Example 2). .

Questions:

  1. How can I search through my code variable which contains $('pre').html(); and store all variable names?

UPDATE 3: Questions

  1. How do I find all desired names
    • Upon finding one:
      1. How do I wrap all occurrences of this word after the point of finding?

I'm not sure if I understood you correctly, but I made the following assumptions according to your question:

  • You are trying to find all variables in a sample text which is actually JavaScript.
  • The matching needs to find variables, which are comma-separated.

If this is, what you need, here is a code snippet in JavaScript which returns a list of variables:

 // extract the variables var regex = /(?:var|const|let)\\s+((?:[^,=]+,)*[^,=]+?)\\s*=/g; var text = document.getElementById('main').innerHTML; var variables = {}; var match = regex.exec(text); while(match !== null) { var variableArraySplitByComma = match[1].split(','); for(var i=0; i<variableArraySplitByComma.length; i++) { variables[variableArraySplitByComma[i]] = true; } var match = regex.exec(text); } // values is an object to keep the values unique var uniqueVariables = Object.keys(variables); console.log(uniqueVariables); 
 #main { white-space: pre; font-family: monospace; } 
 <div id="main"> var test= 123; const hello = "this is a test."; let test2,variable ={'variable':1}; hello.html(); test2.html(); </div> 

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