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
.
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 inr.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.
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). .
code
variable which contains $('pre').html();
and store all variable names? UPDATE 3: Questions
- How do I find all desired names
- Upon finding one:
- 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:
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.