使用 HTML 和 VBscript 替换基于用户输入的单个字符
Replacing single charakters based on user entry with HTML and VBscript
我做了一个回答 ASP,用户可以输入一个数字、一个或多个字符和一个单词。然后他可以按下按钮,它将单词中的给定字符替换为给定的数字。
我的问题是当用户输入更多时,如何让脚本替换单个字符?
即条目是"ab",单词是"abby",数字是“1”,我现在的程序是“1by”,但我想把它变成“111y”,我怎么知道?
<html>
<head>
<meta charset = "utf-8">
<title>Replace
</title>
</head>
<body>
<%
response.flush
l_zahl = request.querystring("f_zahl")
l_wort = request.querystring("f_wort")
Dim letterarray, l_letter
l_letter = request.querystring("f_letter")
letterarry = Split("l_letter")
If IsNumeric(request.querystring("f_zahl")) And Not IsNumeric(request.querystring("f_letter")) And Not IsNumeric(request.querystring("f_wort")) Then
Dim zahlarray, l_zahl
l_zahl = request.querystring("f_zahl")
zahlarry = Split("l_zahl")
Dim wortarray, l_wort
l_wort = request.querystring("f_wort")
wortarry = Split("l_wort")
l_replace = (Replace(l_wort, l_letter, l_zahl, 1, -1, 1))
ElseIf Not IsNumeric(request.querystring("f_zahl")) Then
l_replace = "Keine Zahl"
ElseIf IsNumeric(request.querystring("f_letter")) Then
l_replace = "Kein Buchstabe"
ElseIf IsNumeric(request.querystring("f_wort")) Then
l_replace = "Kein leetspeak"
End If
%>
<form action = "Replacer.asp" method = "get">
<table width = "800" heigth = "400" border="1" cellspacing="0" cellpadding="1" align = "center" font face="tahoma, arial, helvetica, sans-serif" >
<tr>
<td align = "left" width = "100">
Bitte Zahl eingeben
</td>
<td align = "left" width = "100">
<input type = "text" name = "f_zahl" value = "<%=l_zahl%>">
</td>
<td align = "left" width = "100">
Bitte Buchstabe eingeben
</td>
<td>
<input type = "text" name = "f_letter" value = "<%=l_letter%>">
</td>
<td>
Bitte ein Wort eingeben
</td>
<td align = "left" width = "100">
<input type = "text" name = "f_wort" value = "<%=l_wort%>">
</td>
<td width = "*">
<input type = "submit" value = "Ersetzen" \>
</td>
</tr>
<tr>
<td colspan = "2">
</td>
<td >
Verändertes Wort
</td>
<td colspan = "4">
<%=l_replace%>
</td>
</tr>
</table>
</body>
</html>
Replace
function replaces the search string (l_letter
) in the given expression (here l_wort
) with the replacement string (l_zahl
). To replace all characters in l_letter
with l_zahl
you need to do the replacement in a loop for each character in l_letter
. However, the Split
函数不允许您将字符串拆分为字符数组。它在给定的分隔符处拆分字符串(默认为 space)。在没有 space 的变量上调用 Split
将为您提供一个数组,其中只有一个字段包含原始字符串。此外,VBScript 不会扩展字符串中的变量,因此如果将 variable
放在双引号中,您将得到文字字符串 "variable"
,而不是包含变量值的字符串。
var = "ab" : Split("var")
⇒ [ "var" ]
var = "ab" : Split(var)
⇒ [ "ab" ]
var = "a b" : Split(var)
⇒ [ "a", "b" ]
要从字符串中提取单个字符,请使用 Mid
函数:
l_replace = l_wort
For i=1 To Len(l_letter)
l_replace = Replace(l_replace, Mid(l_letter, i, 1), l_zahl)
Next
比在循环中进行多次替换更好的方法是使用 regular expression 替换:
Set re = New RegExp
re.Pattern = "[" & l_letter & "]"
l_replace = re.Replace(l_wort, l_zahl)
将字符输入视为要替换的 list/collection 个字母:
Option Explicit
Dim f : f = "ab"
Dim t : t = "1"
Dim w : w = "abby"
WScript.Echo f, t, w
Dim i
For i = 1 To Len(f)
w = Replace(w, Mid(f, i, 1), t)
Next
WScript.Echo f, t, w
输出:
cscript 47469843.vbs
ab 1 abby
ab 1 111y
我做了一个回答 ASP,用户可以输入一个数字、一个或多个字符和一个单词。然后他可以按下按钮,它将单词中的给定字符替换为给定的数字。 我的问题是当用户输入更多时,如何让脚本替换单个字符? 即条目是"ab",单词是"abby",数字是“1”,我现在的程序是“1by”,但我想把它变成“111y”,我怎么知道?
<html>
<head>
<meta charset = "utf-8">
<title>Replace
</title>
</head>
<body>
<%
response.flush
l_zahl = request.querystring("f_zahl")
l_wort = request.querystring("f_wort")
Dim letterarray, l_letter
l_letter = request.querystring("f_letter")
letterarry = Split("l_letter")
If IsNumeric(request.querystring("f_zahl")) And Not IsNumeric(request.querystring("f_letter")) And Not IsNumeric(request.querystring("f_wort")) Then
Dim zahlarray, l_zahl
l_zahl = request.querystring("f_zahl")
zahlarry = Split("l_zahl")
Dim wortarray, l_wort
l_wort = request.querystring("f_wort")
wortarry = Split("l_wort")
l_replace = (Replace(l_wort, l_letter, l_zahl, 1, -1, 1))
ElseIf Not IsNumeric(request.querystring("f_zahl")) Then
l_replace = "Keine Zahl"
ElseIf IsNumeric(request.querystring("f_letter")) Then
l_replace = "Kein Buchstabe"
ElseIf IsNumeric(request.querystring("f_wort")) Then
l_replace = "Kein leetspeak"
End If
%>
<form action = "Replacer.asp" method = "get">
<table width = "800" heigth = "400" border="1" cellspacing="0" cellpadding="1" align = "center" font face="tahoma, arial, helvetica, sans-serif" >
<tr>
<td align = "left" width = "100">
Bitte Zahl eingeben
</td>
<td align = "left" width = "100">
<input type = "text" name = "f_zahl" value = "<%=l_zahl%>">
</td>
<td align = "left" width = "100">
Bitte Buchstabe eingeben
</td>
<td>
<input type = "text" name = "f_letter" value = "<%=l_letter%>">
</td>
<td>
Bitte ein Wort eingeben
</td>
<td align = "left" width = "100">
<input type = "text" name = "f_wort" value = "<%=l_wort%>">
</td>
<td width = "*">
<input type = "submit" value = "Ersetzen" \>
</td>
</tr>
<tr>
<td colspan = "2">
</td>
<td >
Verändertes Wort
</td>
<td colspan = "4">
<%=l_replace%>
</td>
</tr>
</table>
</body>
</html>
Replace
function replaces the search string (l_letter
) in the given expression (here l_wort
) with the replacement string (l_zahl
). To replace all characters in l_letter
with l_zahl
you need to do the replacement in a loop for each character in l_letter
. However, the Split
函数不允许您将字符串拆分为字符数组。它在给定的分隔符处拆分字符串(默认为 space)。在没有 space 的变量上调用 Split
将为您提供一个数组,其中只有一个字段包含原始字符串。此外,VBScript 不会扩展字符串中的变量,因此如果将 variable
放在双引号中,您将得到文字字符串 "variable"
,而不是包含变量值的字符串。
var = "ab" : Split("var")
⇒ [ "var" ]
var = "ab" : Split(var)
⇒ [ "ab" ]
var = "a b" : Split(var)
⇒ [ "a", "b" ]
要从字符串中提取单个字符,请使用 Mid
函数:
l_replace = l_wort
For i=1 To Len(l_letter)
l_replace = Replace(l_replace, Mid(l_letter, i, 1), l_zahl)
Next
比在循环中进行多次替换更好的方法是使用 regular expression 替换:
Set re = New RegExp
re.Pattern = "[" & l_letter & "]"
l_replace = re.Replace(l_wort, l_zahl)
将字符输入视为要替换的 list/collection 个字母:
Option Explicit
Dim f : f = "ab"
Dim t : t = "1"
Dim w : w = "abby"
WScript.Echo f, t, w
Dim i
For i = 1 To Len(f)
w = Replace(w, Mid(f, i, 1), t)
Next
WScript.Echo f, t, w
输出:
cscript 47469843.vbs
ab 1 abby
ab 1 111y