ToolDeck

String Escape

JavaScript, Python और JSON के लिए स्ट्रिंग को escape और unescape करें

उदाहरण देखें

इनपुट

आउटपुट

स्थानीय रूप से चलता है · सीक्रेट पेस्ट करना सुरक्षित है
परिणाम यहाँ दिखाई देगा…
मोड:

String Escaping क्या है?

String escaping वह प्रक्रिया है जिसमें स्ट्रिंग लिटरल के अंदर विशेष अर्थ रखने वाले वर्णों से पहले एक backslash (या अन्य मार्कर) डाला जाता है। जब कोई प्रोग्रामिंग लैंग्वेज पार्सर को double-quoted स्ट्रिंग के अंदर एक double quote मिलता है, तो वह इसे स्ट्रिंग का अंत मान लेता है। quote को backslash से escape करना — \" लिखना " की जगह — पार्सर को बताता है कि इसे एक literal वर्ण के रूप में माना जाए, न कि delimiter के रूप में। हर वह भाषा जो string literals का उपयोग करती है, उसके escape नियम होते हैं, हालाँकि सटीक sequences अलग-अलग होती हैं।

सबसे सामान्य escape sequences whitespace और control characters को represent करती हैं जो सीधे source code में नहीं आ सकतीं। एक newline \n बन जाता है, एक tab \t बन जाता है, और एक literal backslash \\ बन जाता है। ये परंपराएँ C प्रोग्रामिंग लैंग्वेज (ISO/IEC 9899) से आती हैं और JavaScript (ECMA-262), Python, Java, Go और Rust द्वारा अपनाई गई हैं। JSON (RFC 8259) समान सिंटैक्स का उपयोग करता है लेकिन sequences का एक छोटा सेट समर्थित करता है।

Unescaping (जिसे कभी-कभी "de-escaping" भी कहा जाता है) विपरीत ऑपरेशन है: escape sequences को उनके मूल वर्णों में वापस बदलना। यह log files पढ़ते समय, API responses पार्स करते समय, या ऐसे डेटा को debug करते समय आम है जो double-escaped हो गया हो। हाथ से किए जाने पर दोनों ऑपरेशन थकाऊ होते हैं और इनमें गलतियाँ होना आसान है, इसलिए developers multi-line strings, embedded quotes, या Unicode वर्णों के साथ काम करते समय एक escape/unescape टूल की ओर रुख करते हैं।

Online String Escape टूल का उपयोग क्यों करें?

backslashes को मैन्युअल रूप से जोड़ना या हटाना थकाऊ है और गलती करना आसान है, विशेष रूप से nested quotes या multi-line इनपुट के साथ। एक browser-based string escape टूल REPL सेट अप किए बिना या throwaway scripts लिखे बिना तत्काल परिणाम देता है।

तत्काल रूपांतरण
अपना टेक्स्ट paste करें और escaped या unescaped परिणाम तुरंत पाएँ। Terminal खोलने, REPL शुरू करने, या one-off script लिखने की कोई ज़रूरत नहीं।
🔀
फ़ॉर्मेट के बीच स्विच करें
JavaScript, Python और JSON escaping मोड के बीच टॉगल करें। प्रत्येक भाषा single quotes, Unicode और control characters को अलग तरह से संभालती है — टूल अपने आप सही नियम लागू करता है।
🔒
Privacy-first प्रोसेसिंग
सभी escaping और unescaping JavaScript का उपयोग करके आपके ब्राउज़र में होती है। आपकी strings कभी भी किसी server पर नहीं भेजी जातीं, जो तब महत्वपूर्ण है जब आप API keys, tokens या user data के साथ काम कर रहे हों।
📋
कोई लॉगिन या इंस्टॉल आवश्यक नहीं
पेज खोलें और paste करना शुरू करें। कोई account बनाने की ज़रूरत नहीं, कोई extension install करने की ज़रूरत नहीं, और कोई cookie consent wall टूल को block नहीं करती।

String Escape के उपयोग के मामले

Frontend Development
HTML attributes, inline scripts या template literals में inject करने से पहले user-generated content को escape करें। unescaped quotes या angle brackets से broken markup और XSS vectors को रोकता है।
Backend API Integration
Embedded strings वाले JSON request bodies बनाएँ जिनमें newlines, tabs या quotes हों। उचित escaping malformed JSON को रोकती है जो receiving API से 400 errors का कारण बनता।
DevOps और Configuration
JSON config files, YAML heredocs या environment variables के लिए escaped strings लिखें। Dockerfile ENV या Kubernetes ConfigMap में एक misplaced backslash deployment को तोड़ सकता है।
QA और Test Data
Embedded control characters, Unicode sequences और nested quotes वाले test strings बनाएँ। ये edge-case strings यह verify करने के लिए ज़रूरी हैं कि parsers और serializers special characters को सही तरह से संभालते हैं।
Data Engineering
CSV exports, log aggregators या database dumps से double-escaped data साफ़ करें। जो fields कई serialization layers से गुज़रती हैं उनमें अक्सर extra backslashes जमा हो जाते हैं जिन्हें हटाना पड़ता है।
सीखना और Debugging
देखें कि अलग-अलग भाषाएँ एक ही string को कैसे represent करती हैं। किसी भाषा में नए छात्र और developers JavaScript, Python और JSON escaping को side by side compare करके differences समझ सकते हैं।

Escape Sequence Reference

नीचे दी गई तालिका सामान्य escape sequences और यह सूचीबद्ध करती है कि वे JavaScript, Python और JSON में समर्थित हैं या नहीं। तीनों भाषाएँ core set (newline, tab, backslash, double quote) साझा करती हैं, लेकिन single quotes, hex escapes और extended Unicode notation में भिन्न होती हैं।

Sequenceअर्थJavaScriptPythonJSON
\nNewline (LF)YesYesYes
\rCarriage returnYesYesYes
\tTabYesYesYes
\\BackslashYesYesYes
\"Double quoteYesYesYes
\'Single quoteYesYesNo
\bBackspaceYesYesYes
\fForm feedYesYesYes
\vVertical tabYesYesNo
\0Null characterYesYesNo
\xNNHex byteYesYesNo
\uNNNNUnicode (BMP)YesYesYes
\u{N..}Unicode (full)YesNoNo

JavaScript बनाम Python बनाम JSON Escaping

हालाँकि ये तीन फ़ॉर्मेट समान backslash-based सिंटैक्स साझा करते हैं, ये इस बात में भिन्न होते हैं कि कौन सी sequences वैध हैं और edge cases को कैसे संभालते हैं। गलत मोड चुनने पर ऐसा आउटपुट मिलता है जो सही दिखता है लेकिन parse time पर fail हो जाता है।

JavaScript
\x hex escapes, पूर्ण Unicode range के लिए \u{...} (BMP से परे), vertical tab के लिए \v, और null के लिए \0 को support करता है। Single और double quotes दोनों को escape किया जा सकता है। Template literals (backticks) अधिकांश escaping ज़रूरतों को समाप्त कर देते हैं।
Python
JavaScript जैसी core sequences, plus \x hex escapes और named Unicode characters के लिए \N{name}। Raw strings (r"...") escape processing को पूरी तरह disable कर देते हैं। Single और double quotes दोनों escapable हैं।
JSON
सबसे restrictive फ़ॉर्मेट। केवल \" (double quote), \\, \/, \n, \r, \t, \b, \f, और \uNNNN वैध हैं। कोई single-quote escaping नहीं (JSON strings हमेशा double quotes उपयोग करती हैं)। कोई hex escapes नहीं, कोई \v नहीं, कोई \0 नहीं। कोई भी control character (U+0000 से U+001F) को \uNNNN notation का उपयोग करना होगा।

कोड उदाहरण

JavaScript, Python, Go और command line में strings को escape और unescape करने के उदाहरण।

JavaScript
// Escape a string with special characters
const raw = 'Line 1\nLine 2\tTabbed "quoted"';
const escaped = JSON.stringify(raw);
// → '"Line 1\\nLine 2\\tTabbed \\"quoted\\""'

// Unescape a JSON string value
const input = '"Hello\\nWorld"';
const unescaped = JSON.parse(input);
// → "Hello\nWorld" (actual newline character)

// Template literals don't need quote escaping
const tpl = `She said "hello"`;
// → 'She said "hello"' — no backslashes needed

// Escape for use inside a RegExp
const query = 'price: $5.00 (USD)';
const safe = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// → "price: \\$5\\.00 \\(USD\\)"
Python
# Escape with repr() — shows escape sequences
raw = "Line 1\nLine 2\t'quoted'"
print(repr(raw))
# → "Line 1\nLine 2\t'quoted'"

# Raw strings skip escape processing
path = r"C:\Users\name\Documents"
print(path)
# → C:\Users\name\Documents (backslashes kept literal)

# JSON escaping with json module
import json
data = 'He said "hello\nworld"'
escaped = json.dumps(data)
# → '"He said \\"hello\\nworld\\""'

# Unicode escaping
text = "Caf\u00e9"  # → "Café"
print(text.encode('unicode_escape').decode())
# → "Caf\\xe9"
Go
package main

import (
    "fmt"
    "strconv"
    "encoding/json"
)

func main() {
    // strconv.Quote adds escape sequences and wraps in quotes
    raw := "Line 1\nLine 2\t\"quoted\""
    fmt.Println(strconv.Quote(raw))
    // → "\"Line 1\\nLine 2\\t\\\"quoted\\\"\""

    // strconv.Unquote reverses it
    unescaped, _ := strconv.Unquote(`"hello\nworld"`)
    fmt.Println(unescaped)
    // → hello
    //   world

    // JSON marshal handles escaping automatically
    b, _ := json.Marshal("tabs\there & \"quotes\"")
    fmt.Println(string(b))
    // → "tabs\there \u0026 \"quotes\""
}
CLI (Bash)
# Use $'...' syntax for escape sequences in bash
echo $'Line 1\nLine 2\tTabbed'
# → Line 1
#    Line 2	Tabbed

# printf interprets escape sequences
printf 'Path: C:\\Users\\name\n'
# → Path: C:\Users\name

# Use jq to escape a string for JSON
echo 'He said "hello"' | jq -Rs .
# → "He said \"hello\"\n"

# Unescape JSON string with jq
echo '"Line 1\\nLine 2"' | jq -r .
# → Line 1
#    Line 2

अक्सर पूछे जाने वाले प्रश्न

Escaping और encoding में क्या अंतर है?
Escaping एक string literal के अंदर special characters से पहले backslashes जोड़ता है ताकि language parser उन्हें data के रूप में treat करे, syntax के रूप में नहीं। Encoding पूरी string को एक अलग representation में बदलती है — उदाहरण के लिए, Base64 encoding binary को ASCII text में बदलती है, और URL encoding unsafe characters को percent-hex sequences से बदलती है। Escaping readability बनाए रखती है; encoding format को पूरी तरह बदल देती है।
मेरी string में single के बजाय double backslashes (\\\\ ) क्यों हैं?
Double backslashes तब दिखाई देते हैं जब कोई string दो बार escape की जाती है। यह अक्सर तब होता है जब data कई serialization layers से गुज़रता है — उदाहरण के लिए, एक JSON value जो किसी अन्य JSON string के अंदर stored है, या एक log line जो file में लिखे जाने से पहले JSON-encoded हो गई। इसे ठीक करने के लिए, original content तक पहुँचने तक string को एक-एक layer unescape करें।
JavaScript में JSON के लिए string को कैसे escape करूँ?
JSON.stringify() का उपयोग करें। यह string को double quotes में wrap करता है और JSON द्वारा आवश्यक सभी characters को escape करता है: backslashes, double quotes, newlines, tabs और U+0020 से नीचे के control characters। यदि आप surrounding quotes के बिना केवल inner content चाहते हैं, तो JSON.stringify(str).slice(1, -1) का उपयोग करें।
क्या JSON strings में single quotes हो सकते हैं?
नहीं। JSON specification (RFC 8259) के अनुसार सभी strings को double quotes से delimited होना चाहिए। Single quotes JSON में valid string delimiters नहीं हैं। जबकि कुछ lenient parsers उन्हें accept करते हैं, कोई भी standard-compliant JSON parser single quotes में wrapped string को reject करेगा। यदि आपकी string में single quote character है, तो यह double-quoted JSON के अंदर as-is दिखाई दे सकता है — कोई escaping ज़रूरी नहीं।
Python में raw string क्या है?
एक raw string (r prefix के साथ, जैसे r"C:\\Users\\name") Python interpreter को backslashes को escape markers की जगह literal characters के रूप में treat करने के लिए कहती है। यह Windows file paths, regular expressions और किसी भी string के लिए उपयोगी है जहाँ backslashes preserve किए जाने चाहिए। ध्यान दें कि raw string odd number of backslashes के साथ समाप्त नहीं हो सकती, क्योंकि trailing backslash closing quote को escape कर देगा।
JavaScript में Unicode characters को कैसे escape करूँ?
JavaScript दो Unicode escape forms support करता है। \uNNNN syntax Basic Multilingual Plane (U+0000 से U+FFFF) के characters को संभालता है, जैसे 'e with acute' के लिए \u00e9। BMP से बाहर के characters (emoji, rare scripts) के लिए, \u{NNNNN} का उपयोग करें छह hex digits तक — उदाहरण के लिए grinning face emoji के लिए \u{1F600}। \u{'} form ES2015 में introduce किया गया था।
क्या string escaping security (XSS, SQL injection) से संबंधित है?
Language-level escaping और security escaping अलग-अलग काम करती हैं, लेकिन विचार समान है: special characters को code के रूप में interpret होने से रोकना। XSS prevention के लिए, आप HTML entities (<, >, &) escape करते हैं। SQL injection के लिए, manual escaping की जगह parameterized queries का उपयोग करें। यह टूल language-level string escaping (backslash sequences) को संभालता है, HTML या SQL escaping को नहीं।