2012-11-19 48 views
0

是否有一種方法可以使用批處理文件進行JavaScript代碼查找和替換。這裏有一個例子:使用批處理文件替換Javascript代碼?

查找:<script type="text/javascript" src="http://www.mega.edu/Course/WebCTfooter/footer.js">Footer</script>

替換:<script type="text/javascript" src="http://www.mega.edu/Course/WebCTfooter/footer_ungrad.js">Footer</script>

任何幫助將不勝感激。

+0

是否要更改javascript文本文件?如果是這樣,你可以使用Notepad ++這是一個免費的文本編輯器軟件,它可以讓你在給定的目錄中查找和替換。它應該做你想做的。 – Naner

+0

如果你的機器安裝了perl,python或sed,那將很容易。但我認爲更多的細節會有所幫助 - 例如,爲什麼不在文本編輯器中搜索/替換選項? –

+0

好問題!!我無法在文本編輯器中進行搜索和替換,因爲我有大約25個需要運行的查詢,這些查詢有點類似於上面的查詢。立即做它是一個巨大的痛苦。因此,我正在使用python腳本和批處理來自動執行查找和替換過程 – KingMak

回答

0

既然你可以使用Python,我建議使用Python程序,而不是一個批處理文件,或許是這樣的:

#!/usr/bin/python 

import shutil 
import sys 
import re 

for path in sys.argv[1:]: 
    print "processing " + path 
    infile = open(path,'r') 
    tmpfile = open(path+'.tmp','w') 

    # make the replacements 
    for line in infile: 
     line = re.sub("/footer.js","/footer_ungrad.js",line) 
     line = re.sub("/header.js","/header_ungrad.js",line) 
     line = re.sub("/sample.js","/sample_ungrad.js",line) 
     tmpfile.write(line) 

    infile.close() 
    tmpfile.close() 

    # now move it back to the original file 
    shutil.move(path+'.tmp',path) 

測試使用Python 2.4。

+0

嗯,我想我會用,因爲它的python腳本去使其更易於處理。儘管我已經找到批處理代碼的另一個用法。無論如何謝謝你們幫助我。 – KingMak