Use Replace() to turn the line breaks in a Classic ASP / VBScript string into visible HTML breaks. When text comes from a visitor or another untrusted source, HTML-encode the text first, then replace the newline characters.
<%
Dim textValue, safeText
textValue = "First line" & vbCrLf & "Second line"
safeText = Server.HTMLEncode(textValue)
Response.Write Replace(safeText, vbCrLf, "<br>")
%>
Output:
Server.HTMLEncode() treats the original text as text, not executable HTML. The replacement then introduces only the intended <br> tags.Line breaks created by pressing Enter in a textarea form field are real characters in the submitted text. However, normal HTML rendering collapses most whitespace into a single space. Therefore a string that has several lines in VBScript can appear as one line in a web page.
Use <br> when each original text newline should become a visible break. Do not add a series of <br> elements simply to create spacing in the page layout; use CSS spacing for that.
Different sources can use different end-of-line characters. The following VBScript constants help you identify and work with them:
| Constant | Character(s) | Typical use |
|---|---|---|
vbCrLf | Chr(13) & Chr(10) | Windows-style carriage return followed by line feed |
vbLf | Chr(10) | Unix/Linux-style line feed |
vbCr | Chr(13) | Older Mac-style carriage return |
vbNewLine | Platform-dependent line ending | Convenient in generated VBScript strings; on IIS/Windows, CRLF is typical |
If the string was built on your Windows server, vbCrLf is often sufficient. For text from external systems or files, handle all three variants.
Reference: Microsoft documentation for VB newline constants.
The basic syntax is Replace(expression, find, replace). This replaces all occurrences of the specified line-ending sequence:
Replace(text_variable, vbCrLf, "<br>")The original example works when the input is known to be safe HTML or contains only trusted text. For untrusted text, use the encoded version shown in the next section.
In VBScript, a single quote begins a comment. Write "<br>" using double quotes; '<BR>' is not a string literal.
You can create newline characters directly in VBScript by concatenating vbCrLf:
Dim message
message = "Name: Ava" & vbCrLf & "City: Hyderabad"
Response.Write Replace(Server.HTMLEncode(message), vbCrLf, "<br>")Output: Name: Ava, followed by City: Hyderabad on the next line.
When the text is submitted through Request.Form(), retrieved from a database, or read from a file, treat it as untrusted unless there is a strong reason otherwise. Always apply Server.HTMLEncode() before converting newlines to HTML breaks.
<%
Dim userMessage, encodedMessage
userMessage = Request.Form("message")
encodedMessage = Server.HTMLEncode(userMessage)
Response.Write Replace(encodedMessage, vbCrLf, "<br>")
%>For example, a visitor might submit the literal characters <b>Hello</b>. With HTML encoding, those characters remain visible as text rather than creating a bold element in the resulting document.
<br> and then HTML-encode the result, the browser will display <br> as text instead of a line break.This output-encoding guidance also applies when handling feedback messages and other submitted form values.
A simple replacement of vbCrLf does not handle every possible newline character. The next example converts all three forms to a single vbLf, then replaces that normalized character with <br>.
<%
Dim inputText, safeText
inputText = "First" & vbCrLf & "Second" & vbLf & "Third" & vbCr & "Fourth"
safeText = Server.HTMLEncode(inputText)
safeText = Replace(safeText, vbCrLf, vbLf)
safeText = Replace(safeText, vbCr, vbLf)
safeText = Replace(safeText, vbLf, "<br>")
Response.Write safeText
%>Output:
Order matters: normalize the two-character vbCrLf sequence first. Otherwise replacing vbCr and vbLf separately can turn one CRLF into two HTML breaks.
If you need to reuse this processing, wrap it in a function:
<%
Function TextToHtmlLines(value)
If IsNull(value) Then
TextToHtmlLines = ""
Exit Function
End If
Dim result
result = Server.HTMLEncode(CStr(value))
result = Replace(result, vbCrLf, vbLf)
result = Replace(result, vbCr, vbLf)
TextToHtmlLines = Replace(result, vbLf, "<br>")
End Function
Response.Write TextToHtmlLines("Line one" & vbCrLf & "Line two")
%>This function also handles Null values rather than passing them into Replace(), which would otherwise raise an error.
Save the following as linebreak-demo.asp on an IIS server with Classic ASP enabled. Submit a message containing two or more lines to see the displayed output. The code safely encodes the displayed message and retains line breaks.
<%@ Language="VBScript" %>
<%
Dim message, encodedMessage
message = ""
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
message = Request.Form("message")
End If
encodedMessage = Server.HTMLEncode(message)
%>
<!doctype html>
<html><head><title>Textarea line breaks</title></head><body>
<form method="post" action="linebreak-demo.asp">
<label for="message">Your message</label><br>
<textarea id="message" name="message" rows="5" cols="40"><%= encodedMessage %></textarea><br>
<button type="submit">Show message</button>
</form>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
encodedMessage = Replace(encodedMessage, vbCrLf, vbLf)
encodedMessage = Replace(encodedMessage, vbCr, vbLf)
encodedMessage = Replace(encodedMessage, vbLf, "<br>")
Response.Write "<h2>Submitted text</h2><p>" & encodedMessage & "</p>"
End If
%>
</body></html>The name="message" attribute is what makes the textarea accessible through Request.Form("message"). The form posts to the same example file; change the action value only if you use a different filename.
For a separate form-processing tutorial, see Classic ASP forms and the Request object. To see a previously published example, open the Plus2Net textarea line-break demo.
You do not always need to insert HTML breaks. CSS can preserve line endings while leaving the content HTML-encoded. Use white-space: pre-line when you want the line breaks but not repeated spaces; use white-space: pre-wrap when indentation and repeated spaces should be visible too.
<%
Dim message
message = "First line" & vbCrLf & " Indented second line"
Response.Write "<div style=""white-space:pre-wrap;overflow-wrap:anywhere;"">" & _
Server.HTMLEncode(message) & "</div>"
%>Here, Server.HTMLEncode() protects the original text, and the browser uses CSS to show the newlines. This is particularly convenient for displaying notes, comments, addresses, or formatted text stored in a database.
| Approach | Best suited for |
|---|---|
Replace(..., vbCrLf, "<br>") | Generating HTML with explicit line break elements |
white-space: pre-line | Retaining newlines while collapsing ordinary extra spaces |
white-space: pre-wrap | Preserving both newlines and repeated spaces |
More details are available in the HTML line-break tutorial and the PHP line-break handling tutorial.
The same technique works when FileSystemObject reads a text file. Use a fixed path controlled by the application, check that the file exists, and HTML-encode the contents before displaying them.
<%
Const ForReading = 1
Dim fso, filePath, textFile, contents
filePath = Server.MapPath("notes.txt")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filePath) Then
Set textFile = fso.OpenTextFile(filePath, ForReading)
contents = textFile.ReadAll
textFile.Close
Set textFile = Nothing
contents = Server.HTMLEncode(contents)
contents = Replace(contents, vbCrLf, vbLf)
contents = Replace(contents, vbCr, vbLf)
Response.Write Replace(contents, vbLf, "<br>")
Else
Response.Write "Text file not found."
End If
Set fso = Nothing
%>In this example, notes.txt must exist alongside the .asp script. Do not build a file path directly from a visitor-submitted value.
Use Join() to combine array elements with a newline separator, then display the result:
Dim lines, message
lines = Array("Monday", "Tuesday", "Wednesday")
message = Join(lines, vbCrLf)
Response.Write Replace(Server.HTMLEncode(message), vbCrLf, "<br>")For text that may use mixed newline formats, normalize the newlines and count the parts after Split():
Dim message, normalized, lines
message = "Apple" & vbCrLf & "Banana" & vbLf & "Cherry"
normalized = Replace(message, vbCrLf, vbLf)
normalized = Replace(normalized, vbCr, vbLf)
lines = Split(normalized, vbLf)
Response.Write "Line count: " & (UBound(lines) - LBound(lines) + 1)Output: Line count: 3. An empty input string requires separate handling if you define its line count as zero rather than one.
Two consecutive line-ending sequences represent a blank line. The normalized Replace() technique retains both breaks; CSS pre-wrap can preserve them too.
Dim note
note = "Paragraph one" & vbCrLf & vbCrLf & "Paragraph two"
Response.Write Replace(Server.HTMLEncode(note), vbCrLf, "<br>")Use paragraph tags for structured page content where paragraphs—not raw text lines—are intended. Line-break conversion is most appropriate when you must preserve the original user-entered formatting.
| Problem | Likely cause | Solution |
|---|---|---|
| Text still displays on one line | The source has vbLf rather than vbCrLf | Normalize all newline variants before converting them. |
The page prints <br> literally | Output encoding was applied after inserting HTML breaks | Encode original text first, then replace the line endings. |
| Extra blank lines appear | CR and LF were replaced separately, doubling CRLF line breaks | Convert vbCrLf first. |
| Single-quote syntax causes strange output | VBScript treats ' as a comment delimiter | Use "<br>" for string literals. |
Invalid use of Null error | A missing/nullable value reached Replace() | Check IsNull() and handle the value before replacing. |
| User-supplied HTML renders unexpectedly | The original text was not HTML-encoded | Use Server.HTMLEncode() before adding break tags. |
| Whitespace indentation is lost | Normal HTML rendering collapses repeated spaces | Use white-space: pre-wrap when spacing is important. |
If you are new to the output statement, see Response.Write(). For general text transformations, refer to Replace() and the VBScript string function reference.
vbCrLf automatically create an HTML line break?No. It adds a carriage return and line feed to the underlying string. In normal HTML, these characters do not create a visible line break unless CSS preserves the whitespace or you output an HTML <br> element.
vbCrLf not always enough?Text can come from other operating systems, web services, or files that use only LF or CR characters. Normalizing all three common forms ensures they display consistently.
Server.HTMLEncode() before or after Replace()?For user-controlled text, encode the text before inserting <br> elements. Otherwise either the original HTML may be interpreted by the browser or the intended break tags will be escaped.
white-space: pre-wrap better than Replace()?It depends on the output you need. pre-wrap preserves newlines and repeated spaces without inserting HTML tags. Replace() is appropriate when you specifically need HTML break elements.
Yes. Read Request.Form("message"), encode the value, normalize its newline characters, and then display it using Replace() or CSS. See the complete example above.
Yes. Each newline can become its own <br>; two consecutive newlines create two breaks. However, browser layout may still depend on surrounding markup and CSS.
nl2br() function in VBScript?Classic ASP with VBScript does not provide PHP's nl2br() as a built-in function. Use Replace(), a helper function, or CSS whitespace handling instead.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.