ข้อความ
String Escape
แปลงข้อความเป็น string literal ด้วยการ escape (เครื่องหมายคำพูด แบ็กสแลช อักขระควบคุม) เพื่อวางในโค้ด หรือ unescape กลับ
To put text containing quotes, line breaks, or backslashes inside a string literal in code, those characters need escaping — otherwise they'll end the string early or get misinterpreted. This tool escapes and unescapes text for a specific syntax.
How to use it
- Escape: paste text and pick a target context (a JS string, JSON, shell, etc.) and special characters get replaced with the matching escape sequences.
- Unescape: paste an already-escaped string to get the original text back.
- Different contexts escape the same character differently — make sure you've picked the mode matching where the text will actually be inserted.
Common uses
- Inserting multi-line text or text with quotes into a code variable without manually placing backslashes.
- Preparing a string for a shell command, where quotes and special characters can break execution.
- Unescaping a string copied from logs or a JSON response to read the original text.
Things to keep in mind
The same character (a double quote, for example) is escaped differently depending on context — a JS string, JSON, and a shell each have their own syntax and their own set of special characters.
Double-escaping (escaping already-escaped text) is a common mistake when passing data through multiple layers, like JSON nested inside a shell command.
บทความเกี่ยวกับเครื่องมือนี้: การ Escape สตริง: ทำไมกฎถึงต่างกันใน JS, JSON, shell และ regex
คำถามที่พบบ่อย
การ escape ทำอะไรกันแน่?
แปลงเครื่องหมายคำพูด แบ็กสแลช และอักขระควบคุม (เช่น การขึ้นบรรทัดใหม่หรือแท็บ) เป็นลำดับ escape ที่ถูกต้องภายใน string literal เพื่อให้ข้อความสามารถวางลงในซอร์สโค้ดได้ตามที่เป็นอยู่
ตัวเลือกรูปแบบเครื่องหมายคำพูดและ unicode ใช้ทำอะไร?
รูปแบบเครื่องหมายคำพูด (คู่ เดี่ยว หรือ backtick) กำหนดว่าอักขระใดจะถูก escape เพื่อไม่ให้ทำลาย string และตัวเลือก \uXXXX แปลงอักขระที่ไม่ใช่ ASCII เป็นลำดับ unicode ที่ escape แล้วแทนที่จะปล่อยไว้ตามเดิม
ข้อความที่วางถูกส่งไปยังเซิร์ฟเวอร์หรือไม่?
ไม่ การ escape และการยกเลิก escape ทำงานทั้งหมดในเบราว์เซอร์ผ่าน JavaScript ไม่มีข้อมูลใดถูกส่งออนไลน์
ต้องทำอย่างไรถ้าข้อความต้องผ่านหลายบริบทติดต่อกัน?
ต้อง escape ตามลำดับที่ถูกต้อง: ก่อนอื่นสำหรับบริบทชั้นใน (เช่น JSON) แล้วจึงสำหรับบริบทชั้นนอก (เช่นคำสั่ง shell ที่ JSON นั้นถูกแทรกเข้าไป) การสลับลำดับหรือข้ามชั้นใดชั้นหนึ่งเป็นสาเหตุทั่วไปที่ทำให้สตริงที่ "escape ถูกต้อง" ยังคงพังในทางปฏิบัติ
ทำไมเครื่องหมายคำพูดตัวเดียวกันถึง escape ต่างกันในแต่ละบริบท?
แต่ละบริบท (สตริง JS, JSON, shell, regex) มีไวยากรณ์ของตัวเองพร้อมชุดอักขระพิเศษและวิธี escape ของตัวเอง ดังนั้นการ escape ที่ถูกต้องจึงขึ้นอยู่กับว่าข้อความถูกแทรกไปที่ไหน ไม่ใช่ขึ้นอยู่กับตัวข้อความเอง