gpt4 book ai didi

javascript - jQuery 正则表达式搜索 - 存储所有变量名称

转载 作者:行者123 更新时间:2023-11-30 15:31:33 25 4
gpt4 key购买 nike

我正在为 ECMAScript 5 更新我的 jQuery 语法高亮脚本,并为 JavaScript 语法高亮添加改进。我想搜索我的 code .html();变量并将所有变量名称存储在新变量下 varNames .

变量命名示例:

示例 1).

var a = this;
var b = that;

将“a”和“b”添加到变量名称列表

示例 2).

var a = this,
b = that;

将“a”和“b”添加到变量名称列表

更新:示例 3

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

会将“r”、“x”和“f”添加到变量名称列表中。

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.

搜索变量名:

对于示例1).,更容易获得的变量名,/(var )([a-zA-Z0-9 _]+)/可以使用,但是我使用正则表达式的时间并不长,变量值可以是包含多个逗号的任何值,所以我不确定如何找到示例 2)..

问题:

  1. 如何搜索我的 code包含 $('pre').html(); 的变量并存储所有变量名?

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?

最佳答案

我不确定我是否理解正确,但根据你的问题我做了以下假设:

  • 您正试图在实际上是 JavaScript 的示例文本中查找所有变量。
  • 匹配需要查找变量,以逗号分隔。

如果这是您所需要的,这里是一个 JavaScript 代码片段,它返回一个变量列表:

// 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>

关于javascript - jQuery 正则表达式搜索 - 存储所有变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42181833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com