VBSの質問です

a.htmlをb.htmlへ変換させるvbsを組みたいのですがどうすればいいのでしょうか?
a.html
*********************
<input type="checkbox">
hoge
<input type="checkbox">
hoge
<input type="checkbox">
hoge
<input type="checkbox">
hoge
**********************

b.html
**********************
<input id="box1" type="checkbox">
hoge
<input id="box2" type="checkbox">
hoge
<input id="box3" type="checkbox">
hoge
<input id="box4" type="checkbox">
hoge
**********************
よろしくお願いします

Dim objFile ' 対象ファイル
Dim oldText ' 置換前テキスト
Dim newText ' 置換後テキスト
Dim objRep ' 正規表現オブジェクト
Dim repText ' 置換後文字列
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("a.html")
oldText = objFile.ReadAll
repText = ""
Set objRep = New RegExp
objRep.Multiline = True
objRep.Pattern = "<input "
objRep.Global = True
newText = objRep.replace(oldText, repText)
Set objFile = objFSO.CreateTextFile("b.html")
objFile.WriteLine (newText)

回答の条件
  • 1人5回まで
  • 登録:
  • 終了:2014/02/17 20:10:49
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:taknt No.1

回答回数13539ベストアンサー獲得回数1198

ポイント500pt
Dim objFile  ' 対象ファイル
Dim objFile2 ' 出力ファイル
Dim objFSO ' ファイルシステムオブジェクト
Dim k


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("a.html")
Set objFile2 = objFSO.CreateTextFile("b.html")
k=1
Do Until objFile.AtEndOfLine = True
	strText = objFile.ReadLine
	if instr(strText,"<input ") > 0 then
		strText = replace (strText,"<input ","<input id=""box" & k  & """ ")
		k=k+1
	end if
	objFile2.WriteLine ( strText )
Loop
objFile.Close
objFile2.Close


正規表現で連番を付与させることは できませんので、1行ずつ読んで 変換していくことになります。

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません