Escape Sequence Converter

Overview

This escape sequence converter takes a text string and converts it into their actual characters. It handles common escape sequences such as \n, \", \', and others. For example, if we have:

This is a line\\nThis is another line\\nQuotes: \\\" and \\'

Then, we would expect:

This is a line
This is another line
Quotes: " and '

Online Escape Sequence Converter

Enter your text with escape sequences (e.g., \n, \", \') or regular text you would like escaped below:

Output:

How It Works

The converter uses JavaScript to replace escape sequences with their corresponding characters:

  • \n becomes a newline
  • \" becomes a double quote
  • \' becomes a single quote
  • \\ becomes a single backslash
  • \t becomes a tab
  • \r becomes a carriage return
  • \b becomes a backspace
  • \f becomes a form feed

Any unrecognized escape sequences are left as-is in the output.

Code Implementations

Similar operations are possible using programming languages like R and Python. Below are functions in R and Python that perform the same conversion on text strings.

# Convert escape sequences in text using base R

convert_escapes <- function(text) {
  # Define a list of escape sequences and their replacements
  escapes <- list(
    "\\n" = "\n",
    '\\"' = '"',
    "\\'" = "'",
    "\\\\" = "\\",
    "\\t" = "\t",
    "\\r" = "\r",
    "\\b" = "\b",
    "\\f" = "\f"
  )
  
  # Iterate through the escape sequences and replace them
  for (escape in names(escapes)) {
    text <- gsub(escape, escapes[[escape]], text, fixed = TRUE)
  }
  
  return(text)
}
# Sample input
input_text <- "This is a line\\nThis is another line\\nQuotes: \\\" and \\'"

# Results
cat("Original text:\n", input_text, "\n\n", sep = "")
Original text:
This is a line\nThis is another line\nQuotes: \" and \'
cat("Converted text:\n", convert_escapes(input_text), sep = "")
Converted text:
This is a line
This is another line
Quotes: " and '
# Apply the conversion using stringr
convert_escapes_stringr <- function(text) {
  if(!requireNamespace("stringr", quietly = TRUE)) {
    message("The 'stringr' package is required for this function. Please install it using 'install.packages(\"stringr\")' if you want to use this function.")
    return(text)
  }
  
  # Define the escape sequences and their replacements
  stringr::str_replace_all(text, c(
    "\\\\n" = "\n",
    "\\\\\"" = "\"",
    "\\\\'" = "'",
    "\\\\\\\\" = "\\",
    "\\\\t" = "\t",
    "\\\\r" = "\r",
    "\\\\b" = "\b",
    "\\\\f" = "\f")
  )
}
# Sample input
input_text <- "This is a line\\nThis is another line\\nQuotes: \\\" and \\'"

# Results
cat("Original text:\n", input_text, "\n\n", sep = "")
Original text:
This is a line\nThis is another line\nQuotes: \" and \'
cat("Converted text:\n", convert_escapes_stringr(input_text), sep = "")
The 'stringr' package is required for this function. Please install it using 'install.packages("stringr")' if you want to use this function.
Converted text:
This is a line\nThis is another line\nQuotes: \" and \'
import re

def convert_escapes(text):
    def replace_escape(match):
        escape_char = match.group(1)
        escapes = {
            'n': '\n',
            't': '\t',
            'r': '\r',
            'b': '\b',
            'f': '\f',
            '"': '"',
            "'": "'",
            '\\': '\\'
        }
        return escapes.get(escape_char, match.group(0))

    return re.sub(r'\\(.)', replace_escape, text)

# Sample input
input_text = r"This is a line\nThis is another line\nQuotes: \" and '\nBackslash: \\\nUnrecognized: \z"

# Results
print("Original text:\n", input_text, "\n", sep = '')
Original text:
This is a line\nThis is another line\nQuotes: \" and '\nBackslash: \\\nUnrecognized: \z
print("Converted text:\n", convert_escapes(input_text), sep = '')
Converted text:
This is a line
This is another line
Quotes: " and '
Backslash: \
Unrecognized: \z