Search code examples
replacevbscriptasp-classic

Replacing single characters based on user entry with HTML and VBscript


I made a ans ASP on which a user can enter a number, one or more characters, and a word. Then he can press the button and it replaces in the given characters in the word with the given numbers. My question is how can I let the script replace single characters when the user entered more? i.e. Entry is "ab" the word is "abby", and the number is "1", my current program makes "1by", but I want to make it "111y", how do I realize that?

<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">
            &nbsp;
          </td>
          <td >
            Ver&auml;ndertes Wort
          </td>
          <td colspan = "4">
            <%=l_replace%>
          </td>      
        </tr>
      </table>
  </body>
</html>

Solution

  • The 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 function doesn't allow you to split a string into an array of its characters. It splits a string at a given delimiter character (space by default). Calling Split on a variable without a space will give you an array with just a single field containing the original string. Also, VBScript doesn't expand variables in strings, so if you put variable in double quotes you'll get the literal string "variable", not a string with the value of the variable.

    var = "ab" : Split("var")[ "var" ]
    var = "ab" : Split(var)[ "ab" ]
    var = "a b" : Split(var)[ "a", "b" ]

    For extracting individual characters from a string use the Mid function:

    l_replace = l_wort
    For i=1 To Len(l_letter)
        l_replace = Replace(l_replace, Mid(l_letter, i, 1), l_zahl)
    Next
    

    A better approach than doing multiple replacements in a loop would be using a regular expression replacement:

    Set re = New RegExp
    re.Pattern = "[" & l_letter & "]"
    l_replace = re.Replace(l_wort, l_zahl)