如何过滤掉<script>标记之间的对象

时间:2015-05-23 09:39:49

标签: javascript

我正在使用网络抓取工具来获取数据。现在我有一个包含HTML内容的字符串,我需要获得一个位于标记之间的对象:

&#xA;&#xA;

string

&#xA;&#xA;
  var targetString =&#xA;“&lt; html lang =”en“class =”no-js not-logged-in“&gt;&lt;! - &lt ; [endif]  - &gt;&#xA;&lt; head&gt;&lt; / head&gt;&#xA;&lt; body class =“”&gt; Body Content&#xA;&lt; script type =“text / javascript” &gt; objectName = {foo:5};&lt; / script&gt;&#xA;&lt; / body&gt;&#xA;&lt; / html&gt;&#xA;“; &#xA;  
&#xA;&#xA;

如何从此字符串中取出 objectName ,并将其转换为可读对象,例如我可以用一致的方式称呼'foo'吗?

&#xA;

3 个答案:

答案 0 :(得分:1)

.match()上尝试拨打targetStringRegExp /\{.*\}/作为参数;使用.replace() 0.match()返回的数组的索引RegExp处调用/(\w+)(?=:)/字符串,并使用转义双引号"\"$1\""匹配替换字符串;从JSON.parse()

返回的字符串调用.replace()

&#13;
&#13;
var targetString = ' <html lang="en" class="no-js not-logged-in "> <!--<![endif]-->'
  + '<head></head>'
  + '<body class="">Body Content'
  +  '<script type="text/javascript">objectName = {foo: 5};<\/script>' 
  + '</body>'
  + '</html>'; 

var objectName = JSON.parse(
                   targetString
                   // match left bracket "{" ,
                   // followed by any single character
                   // except the newline characters
                   // 0 or more times
                   // followed by right bracket "}"
                   .match(/\{.*\}/)[0]
                   // match any alphanumeric character
                   // 1 or more times
                   // set replacement string as
                   // captured any alphanumeric character
                   // wrapped within escaped double quotes 
                   // on either side of replacement string
                   .replace(/(\w+)(?=:)/,"\"$1\"")
                 );

console.log(objectName);

document.write(JSON.stringify(objectName, null, 2));

document.write("<br>" + objectName.foo);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用: -

var result=targetstring.split("objectName");
alert(result[1]);

或类似的东西,取决于您的需要;只需使用split()功能。

拆分函数参考:http://www.w3schools.com/jsref/jsref_split.asp

答案 2 :(得分:0)

首先,你问题中的JS代码格式不正确,应该是这样的:

var targetString = 
' <html lang="en" class="no-js not-logged-in "> <!--<![endif]-->\
  <head></head>\
  <body class="">Body Content\
    <script type="text/javascript">objectName = {foo: 5};</script>\
  </body>\
  </html>\
';

如果你确切知道这是你的输入的样子(<script>标签中除了赋值之外没有其他东西),你可以简单地使用正则表达式来提取你的数据对象: / p>

var pattern = /<script [^>]*>[\w_]+\s*=\s*(.*?);?<\/script>/;
eval('(' + targetString.match(pattern)[1] + ')');

如果您只知道脚本中的变量名称,并且其中可能还有其他语句,您可以将整个内容放入<iframe>并提取对象值,如下所示:

var targetString = 
' <html lang="en" class="no-js not-logged-in "> <!--<![endif]-->\
<head></head>\
<body class="">Body Content\
<script type="text/javascript">objectName = {foo: 5};</script>\
</body>\
</html>\
';

var context = document.createElement('iframe');
document.body.appendChild(context);
context.contentWindow.document.write(targetString);
console.log(context.contentWindow.objectName); // got it
document.body.removeChild(context);

See demo here.