ASP 语法
VBScript 基本语法、输出方式、注释、大小写与语句分隔。
三种输出
<%
' 方式一:Response.Write
Response.Write "Hello<br>"
' 方式二:<%= %> 短标签,等价于 Response.Write
%>
<p><%= "Hello" %></p>
<%
' 方式三:直接写在 HTML 里(静态内容)
%>
<p>Hello</p>Hello
Hello
Hello<%= expr %> is shorthand for Response.Write expr. Use the short tag for HTML-heavy output and Response.Write for string building.
注释
<%
' 这是单行注释
' VBScript 没有块注释,多行就写多行单引号
Rem 也可以,但只在一行开头有效
Dim x ' 行尾注释同样可以
%>
<%-- 这不是 VBScript 注释,ASP 里不支持 --%><%-- --%> is JSP syntax; ASP does not recognise it. The contents are treated as server code and raise a syntax error. ASP has only ' and Rem.大小写与语句
<%
Dim UserName
username = "Ada" ' 同一个变量
Response.Write USERNAME ' 输出 Ada
' 一行多条语句用冒号
Dim a, b : a = 1 : b = 2
Response.Write a + b
%>Ada
3VBScript is case-insensitive: UserName, username and USERNAME are one variable. That reads easily but invites typos that silently create new variables — always start with Option Explicit.
Option Explicit
<% Option Explicit %>
<%
Dim total
totl = 10 ' 会报错:变量未声明
total = 10 ' 正确
%>Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'totl'Option Explicit forces declaration, so a typo fails immediately instead of silently creating a variable. Without it totl = 10 creates a new variable and total stays empty — a very hard bug.
字符串与引号
<%
Dim s
s = "He said ""hi""" ' 双引号用两个双引号转义
Response.Write s & "<br>"
Response.Write "共 " & Len(s) & " 个字符<br>"
Response.Write UCase("abc") & "<br>"
Response.Write Replace("a-b-c", "-", "/") & "<br>"
%>He said "hi"
11
ABC
a/b/cVBScript joins strings with &. + also works but does arithmetic when a number appears, so "1" + 2 becomes 3. Always use &.
三种输出
<%
' 方式一:Response.Write
Response.Write "Hello<br>"
' 方式二:<%= %> 短标签,等价于 Response.Write
%>
<p><%= "Hello" %></p>
<%
' 方式三:直接写在 HTML 里(静态内容)
%>
<p>Hello</p>Hello
Hello
Hello<%= expr %> 就是 Response.Write expr 的简写。要输出大量 HTML 时用短标签更清爽,要拼接字符串时用 Response.Write 更方便。
注释
<%
' 这是单行注释
' VBScript 没有块注释,多行就写多行单引号
Rem 也可以,但只在一行开头有效
Dim x ' 行尾注释同样可以
%>
<%-- 这不是 VBScript 注释,ASP 里不支持 --%><%-- --%> 是 JSP 的注释,ASP 不认。写了它,里面的内容会被当成服务端代码执行,直接报语法错误。ASP 只有 ' 和 Rem。大小写与语句
<%
Dim UserName
username = "Ada" ' 同一个变量
Response.Write USERNAME ' 输出 Ada
' 一行多条语句用冒号
Dim a, b : a = 1 : b = 2
Response.Write a + b
%>Ada
3VBScript 不区分大小写,UserName、username、USERNAME 是同一个变量。这让代码读起来轻松,但也容易因为拼写不一致而误建变量——务必在页面顶部写 Option Explicit。
Option Explicit
<% Option Explicit %>
<%
Dim total
totl = 10 ' 会报错:变量未声明
total = 10 ' 正确
%>Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'totl'Option Explicit 强制先声明再使用,拼错变量名会立刻报错而不是悄悄变成空值。不加它,totl = 10 会新建一个变量,total 仍然是空——bug 极难发现。
字符串与引号
<%
Dim s
s = "He said ""hi""" ' 双引号用两个双引号转义
Response.Write s & "<br>"
Response.Write "共 " & Len(s) & " 个字符<br>"
Response.Write UCase("abc") & "<br>"
Response.Write Replace("a-b-c", "-", "/") & "<br>"
%>He said "hi"
11
ABC
a/b/cVBScript 用 & 拼字符串,+ 也能拼但遇到数字会做加法,容易出意外。统一用 & 就不会踩到 "1" + 2 = 3 这种坑。