简体   繁体   English

javascript正则表达式,用于提取字符串中content.html()返回的代码

[英]javascript regular expression to extract the code returned by content.html() in a string

I have a few javascript statements that I need to store in order to implement later in an undo/redo mechanism. 我需要存储一些javascript语句,以便以后在撤消/重做机制中实现。

My statements consist of setting the HTML of the content div, and editing the global variable i . 我的语句包括设置content div的HTML,以及编辑全局变量i $('#content') can consist of many divs within it, each with their own inline style attributes. $('#content')可以包含多个divs ,每个divs具有自己的内联样式属性。 I was previously storing the multiple statements like this: 我以前存储的是以下多个语句:

storedcontent = $('#content').html();
statements = "$('#content').html("+storedcontent+"); i = " +i+ "; ";

and then I would using eval(str); 然后我会使用eval(str); , when the undo button was pressed. ,当按下撤消按钮时。 An example of what might be stored in the statements variable is this: 一个可能存储在statements变量中的示例如下:

content.html('<div id="left" class="margin-line" style="top: 15px; left: 15px; width:1px; height: 317px; border-right:1px dotted gray;"></div>     <div id="top" class="margin-line" style="top: 14px; left: 16px; height:1px; width: 569px; border-bottom:1px dotted gray; "></div>'); 
i = 3;

(Notice that after the .html() statement there's a variable assignment). (注意,在.html()语句之后有一个变量赋值)。

So what I was doing earlier was simply doing eval(statements) , in order to execute both the .html() and the i=3 statements. 因此,我之前所做的只是简单地执行eval(statements) ,以便同时执行.html()i=3语句。

But I've realized eval() had problems. 但是我已经意识到eval()有问题。 So while retaining the two (and there could more) javascript statements in the statements variable, I just want to extract whatever comes between the brackets in $('#content').html() . 因此,虽然在statements变量中保留了两个(可能还有更多)javascript语句,但我只想提取$('#content').html()括号之间的$('#content').html()

So I want a regex to extract from the variable statements , only whatever's returned by $('#content').html() , ie whatever I had in storedcontent . 所以我想要一个正则表达式从变量statements提取,仅$('#content').html()返回的$('#content').html() ,即我存储内容中的storedcontent That way, instead of using eval, I can directly do $('#content').html(storedcontent); 这样,不用使用eval,我可以直接执行$('#content').html(storedcontent);

I know I could easily use substring but I would like to know how to do it using regular expressions. 我知道我可以轻松使用子字符串,但是我想知道如何使用正则表达式。

I've tried to edit the question but I think it might still be confusing. 我试图编辑问题,但我认为它可能仍然令人困惑。 To simplify, perhaps I'm just asking, how, using regex, can we capture the string between two different substrings, in this case $('#content').html( and ); 为了简化,也许我只是问,如何使用正则表达式来捕获两个不同子字符串之间的字符串,在本例中$('#content').html(); ?

I don't really understand why you're not using storedcontent but anyway. 我真的不太明白为什么您不使用storedcontent,而是使用。

What is inside the 'i' variable? “ i”变量中包含什么? If it's an integer, then the RegExp should be: 如果是整数,则RegExp应该为:

var storedcontent2 = statements.match(/#content\'\)\.html\((.*)(\);\s*i\s*=\s*\d+\s*;\s*)$/)[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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