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

标签 javascript jquery regex

我正在为 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/

相关文章:

用于坐标数组的 PHP 正则表达式(或更好的替代方案)

JavaScript RegEx 排除某些单词/短语?

javascript - 多个Component是如何实现的?

javascript - 谷歌图表、PHP 和 Ajax

javascript - 在视频元素顶部移动时,JQuery UI 可调整大小/可拖动丢失 'grip'

javascript - 拖放后向可拖放项目添加标签

javascript - 将sql查询转换为sequelize查询

javascript - 如何计算数组中具有特殊形式的相同项目(javascript)

javascript - 从左向右滑动面板

regex - 以任意顺序查找文本中彼此接近的一组单词