2017-05-05 47 views
1

所以這是類似於我的最後一個問題,但我取得了一些進展。我有一個腳本會收集頁面上的所有javascript並將其發佈到一個php頁面。我想在某種程度上開始解析函數聲明和用法的數據,但嘗試時遇到很多錯誤。以下是我不得不開始:PHP或JS正則表達式來解析JavaScript函數

<script type="text/javascript"> 
$(document).ready(function() { 
var file, S =0; 
var scripts = document.getElementsByTagName('script'); 
function saveJS(JS, FILE){ 
    $.ajax({ 
    type: "POST", 
    url: "/concat.php", 
    data: { 
     scriptText: JS, 
     file: FILE 
    }, 
    success: function(response) { } 
    }); 
} 
function getJS(FILE){ 
    var response; 
    $.get(FILE, function(data) { 
     saveJS(data, FILE) 
    }); 

} 

function postLoop() {   
    setTimeout(function() { 
     if (S < scripts.length) {   
     if (scripts[S].src) { 
      getJS(scripts[S].src) 
     }else{ 
      saveJS(scripts[S].innerHTML, "inline"); 
     }  
     S++;    
     postLoop();    
     }  
    }, 4000) 
} 
postLoop(); 
}); 
</script> 

<?php 
$file = 'scripts.js'; 
$JS = $_REQUEST["scriptText"]; 
$contents = " 

// Source File: " . $_REQUEST["file"] . " // 

" . $JS ." 

// End File: " . $_REQUEST["file"] . " // 

"; 
preg_replace("#([\n\r]{1,2})#", "\r\n", $contents); 
file_put_contents($file, $contents, FILE_APPEND | LOCK_EX); 
?> 

這將產生具有javascript代碼的每一行的單個.js文件 - 無論是書面的直列或從遠程.js文件中拉出。正如你所期望的那樣,將會有單引號和雙引號的混合,甚至可能反覆出現在這裏和那裏,這意味着可能需要一些HTML特殊字符轉義或什麼。

使用PHP我一直無法找出任何方式使用正則表達式:

preg_match_all(function.*\(.*\).*\{(.|\n)*?\n\}, $contents, 
    $out, PREG_PATTERN_ORDER); 
print_r($out); 

preg_match_all('function.*\(.*\).*\{(.|\n)*?\n\}', $contents, 
    $out, PREG_PATTERN_ORDER); 
print_r($out); 

$matches = array(); 
preg_match(`function.*\(.*\).*\{(.|\n)*?\n\}`, $contents, $matches); 
print_r($matches); 

$split = preg_split("function.*\(.*\).*\{(.|\n)*?\n\}", $contents); 
print_r($split); 

這些都不返回任何結果:

function.*\(.*\).*\{(.|\n)*?\n\} 

我已經使用像試過,所以我在Javascript中嘗試了類似的東西,並且空了。我的最終文件大約是2.5Mb,所以我認爲PHP會更好,任何提示?

+3

用正則表達式解析JavaScript?因爲JS不是[常規語言](https://en.wikipedia.org/wiki/Regular_language),所以在這方面算是不可能的。 –

+2

我可以發誓,這是不可能的。如何:'function(){return「}」; }'和'function(){return「\」「;}','function(a =」)「){}'... –

+0

Thanks guys,the regex' function。* \(。* \)。* \ {(。| \ n)*?\ n \}'在Sublime和Editplus中很好用,我不認爲這對PHP來說是不可能的,因爲基本上這就是縮小器和uglifier所做的,對吧? – Alan

回答

0

工作了很長時間後,我開始工作了。它匹配一些我希望它不會的東西,但最終我認爲我可以解決它,因爲它後來結果爲空結果。

這裏是PHP

$html = file_get_contents('https://raw.githubusercontent.com/devongovett/regexgen/master/src/regex.js'); 
$regex = "/((function\s.*\(.*\)\s)?({(?:[^\{\}]++|(?R))*\}))/"; 
preg_match_all($regex, $html, $tmps, PREG_PATTERN_ORDER); 
// here you could dump $tmps to see what it matches 
echo "<pre>"; 

foreach($tmps[0] as $line){ 
$regex2 = "/function\s(.*)\(.*\)/"; 
preg_match_all($regex2, $line, $tmps2); 

var_dump($tmps2[1]); 
} 
echo "</pre>"; 

這將拉動JS文件,並帶回函數名稱:

array(0) { 
} 
array(0) { 
} 
array(0) { 
} 
array(1) { 
    [0]=> 
    string(7) "toRegex" 
} 
array(1) { 
    [0]=> 
    string(4) "star" 
} 
array(1) { 
    [0]=> 
    string(5) "union" 
} 
array(1) { 
    [0]=> 
    string(21) "removeCommonSubstring" 
} 
array(1) { 
    [0]=> 
    string(15) "commonSubstring" 
} 
array(1) { 
    [0]=> 
    string(6) "concat" 
} 

當然還有更多,你可以用它做什麼,但是這將爲我的項目提供一個良好的開端。