replace() เป็น JavaScript string method มาตรฐานที่ใช้ใน n8n expressions สำหรับแทนที่ข้อความหรือ regex pattern ตัวแรกที่พบในข้อความเท่านั้น จุดสำคัญที่ต้องจำคือ มันแทนที่แค่ตัวแรก ไม่ได้แทนที่ทุกตัวที่เจอ method นี้รองรับทั้ง string literal และ regex pattern พร้อม capturing groups เช่น $1, $2 สำหรับการแทนที่ข้อความที่ซับซ้อนและจัดเรียงข้อมูลใหม่ได้ ใช้บ่อยใน n8n workflows สำหรับการทำความสะอาดข้อมูลจาก webhook หรือ API response การลบคำนำหน้าหรือคำต่อท้าย การแก้ไขรูปแบบ URL ก่อนเรียก HTTP Request และการปรับแต่งข้อความก่อนส่งต่อไปยัง API หรือ database
=string.replace(searchValue, replaceValue)
=string.replace(searchValue, replaceValue)
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| searchValue | string|regex | Yes | ข้อความหรือ regex pattern ที่ต้องการค้นหาและแทนที่ ถ้าเป็น string จะแทนที่เฉพาะตัวแรกแบบ case-sensitive ถ้าเป็น regex สามารถใช้ flags เช่น /i (ignore case) หรือ /g (global) ได้ | |
| replaceValue | string|function | Yes | ข้อความใหม่ที่จะนำมาแทนที่ รองรับ special patterns เช่น $& (matched text), $1 $2 (capturing groups), $` (text before match), $’ (text after match) หรือสามารถเป็น function สำหรับการแปลงแบบ dynamic |
ลบคำนำหน้าหรือคำต่อท้ายที่ไม่ต้องการออกจากข้อมูล เช่น ลบ 'ID_' จาก 'ID_12345', ลบ '.tmp' จากชื่อไฟล์ เหมาะสำหรับทำความสะอาดข้อมูลก่อนส่งเข้า database
แทนที่ตัวคั่นหรือรูปแบบข้อมูลให้ตรงกับมาตรฐาน เช่น แทนที่ space ตัวแรกด้วย underscore, เปลี่ยน date format จาก '/' เป็น '-', แก้ไข URL path ให้ถูก format
แก้ไขการสะกดผิดหรือแทนที่คำที่ไม่เหมาะสมในข้อความ เช่น แก้ชื่อประเทศที่สะกดผิด, แทนที่คำหยาบด้วย *, แก้ไข typo ที่พบบ่อย
แก้ไข URL หรือ file path ให้ตรงกับ environment ต่างๆ เช่น แทนที่ 'http://' เป็น 'https://', เปลี่ยน base URL ตัวแรก, แก้ไข domain name
{{ 'apple banana apple cherry'.replace('apple', 'orange') }}={{ 'apple banana apple cherry'.replace('apple', 'orange') }}
orange banana apple cherry
{{ $json.product_code.replace('SKU_', '') }}={{ $json.product_code.replace('SKU_', '') }}
12345
{{ $json.full_name.replace(/(\w+)\s(\w+)/, '$2, $1') }}={{ $json.full_name.replace(/(\w+)\s(\w+)/, '$2, $1') }}
Smith, John
{{ $json.url.replace('http://', 'https://').replace(/\/api\/v1\//, '/api/v2/').trim() }}={{ $json.url.replace('http://', 'https://').replace(/\/api\/v1\//, '/api/v2/').trim() }}
https://example.com/api/v2/users
ใช่ครับ ถูกต้อง 100% 😅
.
เมื่อใช้ string literal เป็น searchValue มันจะแทนที่เฉพาะตัวแรกเท่านั้น
.
ตัวอย่าง: ‘aaa’.replace(‘a’, ‘b’) จะได้ ‘baa’ (แทนที่แค่ a ตัวแรก)
.
ถ้าต้องการแทนที่ทุกตัวมี 2 วิธี:
• วิธีที่ 1: ใช้ replaceAll() → ‘aaa’.replaceAll(‘a’, ‘b’) → ‘bbb’
• วิธีที่ 2: ใช้ regex กับ /g flag → ‘aaa’.replace(/a/g, ‘b’) → ‘bbb’
.
ส่วนตัวผมแนะนำให้ใช้ replaceAll() ตรงๆ จะอ่านง่ายกว่า แต่ถ้าต้องการความยืดหยุ่นมากกว่า (เช่น ignore case) ก็ใช้ regex ดีกว่าครับ 😎
นี่เป็นคำถามที่เจอบ่อยมากครับ 😅 มาดูความต่างกันแบบชัดๆ เลย:
.
**replace() – แทนที่เฉพาะตัวแรก**
‘cat cat cat’.replace(‘cat’, ‘dog’) → ‘dog cat cat’
.
**replaceAll() – แทนที่ทุกตัวที่พบ**
‘cat cat cat’.replaceAll(‘cat’, ‘dog’) → ‘dog dog dog’
.
**ถ้าใช้ regex กับ /g flag:**
‘cat cat cat’.replace(/cat/g, ‘dog’) → ‘dog dog dog’ (เหมือน replaceAll)
.
💡 **Tip:** ใช้ replace() เมื่อต้องการแทนที่เฉพาะตัวแรก เช่น ลบ prefix หรือแก้ไขครั้งเดียว ส่วน replaceAll() ใช้เมื่อต้องการแทนที่ทั้งหมด เช่น ลบ space หรือแก้ไข typo ทุกจุดในข้อความ
รองรับเต็มที่เลยครับ 😎 replace() ใช้ได้ทั้ง string literal และ regex patterns:
.
**String Literal (แทนที่แค่ตัวแรก):**
$json.text.replace(‘old’, ‘new’)
.
**Regex Pattern (แทนที่ pattern ซับซ้อน):**
• $json.text.replace(/\d+/, ‘NUM’) → แทนที่ตัวเลขกลุ่มแรก
• $json.text.replace(/\s+/g, ‘ ‘) → แทนที่ช่องว่างหลายตัวเป็นช่องว่างเดียว
• $json.text.replace(/[aeiou]/gi, ‘*’) → แทนที่สระทั้งหมด
.
**Regex Flags ที่ใช้ได้:**
• /g = global (แทนที่ทุกตัว)
• /i = ignore case (ไม่สนใจตัวพิมพ์เล็กใหญ่)
• /m = multiline
.
💡 ส่วนตัวผมแนะนำให้ test regex pattern ก่อนใช้งานจริง ใช้เว็บ regex101.com ช่วยได้เยอะครับ จะได้ไม่ต้องมานั่งแก้ใน workflow แล้วเจอ error ทีหลัง 555
นี่เป็นฟีเจอร์ที่เจ๋งมากครับ 😎 Capturing groups ใช้จับข้อมูลจาก regex แล้วนำกลับมาใช้ใน replacement string ได้:
.
**Special Patterns ที่ใช้ได้:**
• $1, $2, … $n = กลุ่มที่จับได้ (1st, 2nd, … nth group)
• $& = ข้อความที่ match ทั้งหมด
• $` = ข้อความก่อน match
• $’ = ข้อความหลัง match
.
**ตัวอย่างการใช้งานจริง:**
.
1️⃣ สลับลำดับคำ:
‘John Doe’.replace(/(\w+) (\w+)/, ‘$2, $1’) → ‘Doe, John’
.
2️⃣ Format เบอร์โทรศัพท์:
‘0812345678’.replace(/(\d{3})(\d{3})(\d{4})/, ‘$1-$2-$3’) → ‘081-234-5678’
.
3️⃣ Extract และจัดรูปแบบ:
‘Price: $100’.replace(/\$(\d+)/, ‘USD $1.00’) → ‘Price: USD 100.00’
.
💡 **ประสบการณ์ส่วนตัว:** ผมใช้เทคนิคนี้บ่อยมากครับ ช่วยแปลงข้อมูลได้โดยไม่ต้อง split แล้ว concat หลายครั้ง ทำให้ code สั้นลงและอ่านง่ายขึ้นเยอะเลย
จากประสบการณ์ผม replace() ใช้ได้ในหลายสถานการณ์ครับ มาดูตัวอย่างที่เจอบ่อยๆ:
.
**1️⃣ Data Cleaning:**
• ลบ prefix/suffix: $json.code.replace(‘PREFIX_’, ”)
• แก้ไข typo: $json.text.replace(‘teh’, ‘the’)
.
**2️⃣ URL/Path Transformation:**
• อัพเกรด protocol: $json.url.replace(‘http://’, ‘https://’)
• แก้ไข domain: $json.api_url.replace(‘staging.’, ‘production.’)
.
**3️⃣ Format Conversion:**
• เปลี่ยน date separator: $json.date.replace(‘/’, ‘-‘)
• แก้ไข line breaks: $json.text.replace(‘
‘, ‘
‘)
.
**4️⃣ Conditional Text:**
• ซ่อนข้อมูลส่วนบุคคล: $json.email.replace(/@.*/, ‘@***’)
• Mask credit card: $json.card.replace(/\d{12}/, ‘************’)
.
**ใช้ใน Node Types:**
• Set node → แปลงข้อมูลก่อนส่งต่อ
• IF node → เช็ค condition หลังแทนที่
• Code node → รวมกับ JavaScript logic อื่นๆ
.
💡 ส่วนตัวผมใช้ใน Set node บ่อยที่สุด เพราะง่ายและเห็นผลทันทีครับ 😎
replace() เป็น JavaScript string method มาตรฐานที่ใช้ได้ใน n8n expressions สำหรับแทนที่ข้อความหรือ regex pattern ตัวแรกที่พบใน string ด้วยข้อความใหม่ที่ระบุ
.
ที่ต้องจำให้ขึ้นใจคือ มันแทนที่แค่ตัวแรกที่เจอเท่านั้นนะครับ ถ้าต้องการแทนที่ทั้งหมด ต้องใช้ replaceAll() หรือ regex กับ /g flag แทน 😎
.
วิธีนี้เหมาะสำหรับการลบคำนำหน้าหรือคำต่อท้ายที่ไม่ต้องการ แก้ไขข้อผิดพลาดในข้อความ ปรับรูปแบบข้อมูลในตำแหน่งที่พบครั้งแรก และใช้แปลงข้อมูลก่อนส่งต่อไปยัง node ถัดไป
.
ที่เจ๋งคือมันรองรับทั้ง string literal และ regex pattern พร้อม capturing groups ($1, $2) ด้วย ทำให้สามารถจัดการข้อความที่ซับซ้อนได้ครับ 💡