String Escape / Unescape
Escape and unescape strings for JSON, JavaScript, and HTML contexts.
StringEscape.jsonDesc
StringEscape.cheatsheet
\β\\"β\"newlineβ\ntabβ\tcarriage returnβ\rbackspaceβ\bform feedβ\fFrequently Asked Questions
Code Implementation
import json
import html
import re
text = 'Hello "world"\nLine 2 & <tag>'
# JSON string escaping
json_escaped = json.dumps(text)
print(json_escaped)
# '"Hello \"world\"\nLine 2 & <tag>"'
# Python repr (Python string literal escaping)
py_escaped = repr(text)
print(py_escaped)
# '\'Hello "world"\nLine 2 & <tag>\''
# HTML escaping
html_escaped = html.escape(text)
print(html_escaped)
# 'Hello "world"\nLine 2 & <tag>'
# Regex escaping (escape metacharacters for use in pattern)
regex_escaped = re.escape("price: $4.99 (USD)")
print(regex_escaped)
# 'price:\ \$4\.99\ \(USD\)'Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.