24-6 ?用表示法??用

b WSH ]iHϥγqΪܪkA覡iHڭ̫ܧ֦abjqrڭ̩ҭnTCUoӽdҡAڭ̧X@ӺDG

lɡ]regExp01.js^G]ǦϰUYi^
// ϥγqΪܪkX@ӺD

// ŪwФ
localFile="test.htm"
fso = new ActiveXObject("Scripting.FileSystemObject");
forReading=1, forWriting=2;
fid=fso.OpenTextFile(localFile, forReading);
contents=fid.ReadAll();
fid.Close();
//WScript.Echo(content);

// qιB⦡
myRegExp = /<title>(.*)<\/title>/i;
title = myRegExp.exec(contents);

// ܵG
WScript.Echo("D = " + title[1]);
WScript.Echo("D = " + RegExp.$1);

{X|q test.htm XDALXGpUG

D = ߲MؤjǸTu{Ǩt D = ߲MؤjǸTu{Ǩt bWzdҤAtitle[1] M RegExp.$1 xsۦPGCHP˪覡Aڭ̤]iHX@ӥH JScript D WSH ɮתĤ@CѡApUG

lɡ]regExp02.js^G]ǦϰUYi^
// X WSH {X]JScript^Ĥ@ѦC

// Ūɮ
localFile=WScript.ScriptFullName;
fso = new ActiveXObject("Scripting.FileSystemObject");
forReading=1, forWriting=2;
fid=fso.OpenTextFile(localFile, forReading);
contents=fid.ReadAll();
fid.Close();
//WScript.Echo(content);

// qιB⦡
myRegExp = /\s*\/\/\s*(.*)/;
firstCommentLine = myRegExp.exec(contents);

// LXG
WScript.Echo("Ĥ@ѦC = " + firstCommentLine[1]);

LXGOG

Ĥ@ѦC = X WSH {X]JScript^Ĥ@ѦC QάۦP覡Aڭ̥iHdҥؿs@@ index.asp ɮסAiHYɦaNC@ js ɮתĤ@CѦCLXӡAΦ@ WSH Ҧ JScript dҪCM²满ApUG

Example]index.asp^G

WzdҪlɦpUG

lɡ]index.asp^G]ǦϰUYi^
<%@language=JScript%>
<% title=Request.ServerVariables("SCRIPT_NAME") %>

<%
function getFirstComment(fileName){
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var fid=fso.OpenTextFile(fileName, 1);
	var line=fid.ReadLine();
	fid.Close();
	
	var pattern = /\s*\/\/\s*(.*)$/;
	var abc = pattern.exec(line);
	if (abc==null)
		return("");
	else
		return(RegExp.$1);	// άOureturn(abc[1]);vi
}

// List files in a given directory with a given extension
function fileList(directory, extension){
	fso = new ActiveXObject("Scripting.FileSystemObject");
	fd = fso.GetFolder(Server.MapPath(directory));
	fc = new Enumerator(fd.Files);
	fileNames=new Array();
	var i=0;
	for (; !fc.atEnd(); fc.moveNext()){
		fileName=fc.item()+"";
		items=fileName.split(".");
		ext=items[items.length-1];	// Get file extension
		if (arguments.length==2)
			if ((ext.toUpperCase()==extension.toUpperCase())||(ext.toLowerCase()==extension.toLowerCase()))
				fileNames[i++]=fileName;
		if (arguments.length==1)
			fileNames[i++]=fileName;
	}
	return(fileNames.sort());
}
%>
<html>
<head>
	<title><%=title%></title>
	<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
	<meta HTTP-EQUIV="Expires" CONTENT="0">
	<style>
		td {font-family: "з", "helvetica,arial", "Tahoma"}
		A:link {text-decoration: none}
		A:hover {text-decoration: underline}
	</style>
</head>

<body background="/jang/graphics/background/yellow.gif">
<font face="з">
<h2 align=center><%=title%></h2>
<hr>

<table border=1 align=center>
<tr>
<th>ɮצW<th><th>ɮפjp]Bytes^<th>̫קɶ
<%
files=fileList(".", "js");
fso = new ActiveXObject("Scripting.FileSystemObject");
for (i=0; i<files.length; i++){
	f = fso.GetFile(files[i]);
	Response.write("<tr>");
	Response.write("<td><a href=\"" + f.Name + "\">" + f.Name + "</a></td>");
	Response.write("<td>" + getFirstComment(files[i]) + "&nbsp;</td>");
	Response.write("<td>" + f.Size + "</td>");
	Response.write("<td>" + f.DateLastModified + "</td>");
}
%>
</table>

<hr>

<script language="JavaScript">
document.write("Last updated on " + document.lastModified + ".")
</script>

<a href="/jang/sandbox/asp/lib/editfile.asp?FileName=<%=Request.ServerVariables("PATH_INFO")%>"><img align=right border=0 src="/jang/graphics/invisible.gif"></a>
</font>
</body>
</html>

ڭ̤]iHw@q HTML lXAis}MsrG

lɡ]linkExtraction01.js^G]ǦϰUYi^
// q@qrAsrP}

content="<a href=\"url1\">text1</a> and <a href=\"url2\">text2</a> and \r\n<a href=\"url3\">text3</a>";
pattern=/<A(.*?)<\/A>/gi;
found=content.match(pattern);
pattern2=/<\s*A\s+HREF\s*=\s*"?(.*?)"?\s*>(.*?)<\s*\/\s*A\s*>/i;
for (i=0; i<found.length; i++){
	pattern2.exec(found[i]);
	WScript.Echo(found[i]+" ===> URL="+RegExp.$1+", TEXT="+RegExp.$2);
}

ܵGpUG

<a href="url1">text1</a> ===> URL=url1, TEXT=text1 <a href="url2">text2</a> ===> URL=url2, TEXT=text2 <a href="url3">text3</a> ===> URL=url3, TEXT=text3 ڭ̤]iHw@Ӻ test.htm өs}MsrG

lɡ]linkExtraction02.js^G]ǦϰUYi^
// q@ɮפAsrP}]\णAiAiI^

fileName="test.htm";
fso = new ActiveXObject("Scripting.FileSystemObject");
fid=fso.OpenTextFile(fileName, 1);
content=fid.ReadAll();
fid.Close();

pattern=/<A(.*?)<\/A>/gi;
found=content.match(pattern);
pattern2=/<\s*A\s+HREF\s*=\s*"?(.*?)"?\s*>(.*?)<\s*\/\s*A\s*>/i;
for (i=0; i<found.length; i++){
	pattern2.exec(found[i]);
	WScript.Echo(found[i]+" ===> URL="+RegExp.$1+", TEXT="+RegExp.$2);
}

ܵGpUG

<a href="2001_need_teacher_c.doc" target="_blank">()</a> ===> URL=2001_need_teacher_c.doc" target="_blank, TEXT=() <a href="2001_need_teacher_e.doc" target="_blank">(^)</a> ===> URL=2001_need_teacher_e.doc" target="_blank, TEXT=(^) <a href="/intro.html"><img src="/icon/csbuild_8.jpg" lowsrc="/icon/csbuild_7b.jpg" border=0 alt="MظT²v"></a> ===> URL=/intro.html, TEXT=<img src="/icon/csbuild_8.jpg" lowsrc="/icon/csbuild_7b.jpg" border=0 alt="MظT²v"> <a href="Grading_report.doc">tҦۧڵŲi</a> ===> URL=Grading_report.doc, TEXT=tҦۧڵŲi <a href="mailto:www@cs.nthu.edu.tw">N P </a> ===> URL=mailto:www@cs.nthu.edu.tw, TEXT=N P <a href="/webteam">s @ p </a> ===> URL=/webteam, TEXT=s @ p <a href="special.html"><font face="Arials"> N V & h</a> ===> URL=special.html, TEXT=<font face="Arials"> N V & h

Hint
bդWzdҮɡAаOonN{X linkExtraction02.js Mɮ test.htm bP@ӥؿC


JScript {]pPΡGΩ WSH