---
title: replace – แทนที่ข้อความหรือ pattern ตัวแรกที่พบใน string
url: https://www.thepexcel.com/functions/n8n/string-functions/replace-n8n/
type: function-explainer
program: n8n
syntax: "string.replace(searchValue, replaceValue)"
date: 2025-12-18
scores:
  popularity: 8
  difficulty: 3
  usefulness: 8
---

# replace – แทนที่ข้อความหรือ pattern ตัวแรกที่พบใน string

> แทนที่ข้อความหรือ regex pattern ตัวแรกที่พบใน string

## คำอธิบาย

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

## Syntax

```excel
string.replace(searchValue, replaceValue)
```

**Variant**

```excel
{{ $json.field.replace('search', 'replacement') }}
```

แทนที่ string literal ตัวแรกที่พบ (case-sensitive)

**Variant**

```excel
{{ $json.field.replace(/pattern/, 'replacement') }}
```

แทนที่ regex pattern ตัวแรกที่พบ (ไม่มี /g flag)

**Variant**

```excel
{{ $json.field.replace(/pattern/g, 'replacement') }}
```

แทนที่ regex pattern ทุกตัวที่พบ (ใช้ /g flag แทน replaceAll)

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| searchValue | Yes | string\|regex |  | ข้อความหรือ regex pattern ที่ต้องการค้นหาและแทนที่ ถ้าเป็น string จะแทนที่เฉพาะตัวแรกแบบ case-sensitive ถ้าเป็น regex สามารถใช้ flags เช่น /i (ignore case) หรือ /g (global) ได้ |
| replaceValue | Yes | string\|function |  | ข้อความใหม่ที่จะนำมาแทนที่ รองรับ special patterns เช่น $& (matched text), $1 $2 (capturing groups), $` (text before match), $' (text after match) หรือสามารถเป็น function สำหรับการแปลงแบบ dynamic |

## เคสการใช้งาน

### Data Cleaning - ลบ Prefix/Suffix

ลบคำนำหน้าหรือคำต่อท้ายที่ไม่ต้องการออกจากข้อมูล เช่น ลบ 'ID_' จาก 'ID_12345', ลบ '.tmp' จากชื่อไฟล์ เหมาะสำหรับทำความสะอาดข้อมูลก่อนส่งเข้า database

_เหมาะกับ:_ data-cleaning

### Text Normalization - ปรับรูปแบบข้อมูล

แทนที่ตัวคั่นหรือรูปแบบข้อมูลให้ตรงกับมาตรฐาน เช่น แทนที่ space ตัวแรกด้วย underscore, เปลี่ยน date format จาก '/' เป็น '-', แก้ไข URL path ให้ถูก format

_เหมาะกับ:_ format-normalization

### Error Correction - แก้ไขข้อผิดพลาดในข้อความ

แก้ไขการสะกดผิดหรือแทนที่คำที่ไม่เหมาะสมในข้อความ เช่น แก้ชื่อประเทศที่สะกดผิด, แทนที่คำหยาบด้วย *, แก้ไข typo ที่พบบ่อย

_เหมาะกับ:_ text-correction

### URL/Path Transformation - แปลง URL หรือ File Path

แก้ไข URL หรือ file path ให้ตรงกับ environment ต่างๆ เช่น แทนที่ 'http://' เป็น 'https://', เปลี่ยน base URL ตัวแรก, แก้ไข domain name

_เหมาะกับ:_ url-transformation

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: การแทนที่ String ตัวแรกเท่านั้น (Basic)

```excel
{{ 'apple banana apple cherry'.replace('apple', 'orange') }}
```

**ผลลัพธ์:** `orange banana apple cherry`

นี่คือพฤติกรรมพื้นฐานของ replace() ที่ต้องจำให้ขึ้นใจครับ 😅
.
สังเกตว่า มันแทนที่เฉพาะคำว่า 'apple' ตัวแรกที่เจอเท่านั้น ส่วนคำว่า 'apple' ตัวที่สองที่อยู่หลัง banana ยังคงเป็น 'apple' อยู่เหมือนเดิม ไม่เปลี่ยนแปลง
.
💡 **Tip จากผม:** ถ้าต้องการแทนที่คำว่า 'apple' ทุกตัวที่พบ มีทางเลือก 2 วิธี:
• วิธีที่ 1: ใช้ replaceAll('apple', 'orange') แทน
• วิธีที่ 2: ใช้ replace(/apple/g, 'orange') โดยใส่ /g flag
.
ทั้ง 2 วิธีจะได้ผลลัพธ์เป็น 'orange banana orange cherry' ครับ 😎

### 2. ตัวอย่างที่ 2: ลบ Prefix ใน n8n Workflow (Practical)

```excel
{{ $json.product_code.replace('SKU_', '') }}
```

**ผลลัพธ์:** `12345`

สมมติว่าข้อมูลที่เข้ามาจาก previous node มีค่า $json.product_code = 'SKU_12345'
.
การแทนที่ 'SKU_' ด้วย empty string ('') จะทำให้คำนำหน้าถูกลบออกไป เหลือเฉพาะตัวเลข '12345' เท่านั้น
.
🔧 **ตัวอย่างการใช้งานจริง:**
.
**ขั้นตอนที่ 1:** Webhook node รับข้อมูล product code จาก external system ที่มาพร้อมคำนำหน้า 'SKU_'
.
**ขั้นตอนที่ 2:** ใช้ Set node กับ expression {{ $json.product_code.replace('SKU_', '') }} เพื่อลบคำนำหน้าออก
.
**ขั้นตอนที่ 3:** ส่งต่อเฉพาะตัวเลข product code ไปยัง HTTP Request node เพื่อเรียก API
.
💡 เทคนิคนี้ผมใช้บ่อยมากครับ เพราะข้อมูลที่เข้ามาจาก external system มักจะมี prefix หรือ suffix ที่ไม่ต้องการ ต้องทำความสะอาดก่อนส่งต่อเสมอ 😅

### 3. ตัวอย่างที่ 3: ใช้ Regex กับ Capturing Groups (Advanced)

```excel
{{ $json.full_name.replace(/(\w+)\s(\w+)/, '$2, $1') }}
```

**ผลลัพธ์:** `Smith, John`

สมมติว่าข้อมูลที่เข้ามามีค่า $json.full_name = 'John Smith'
.
**มาดูว่า Regex ทำงานยังไง:**
.
Pattern /(\w+)\s(\w+)/ แบ่งออกเป็น 2 capturing groups:
• (\w+) = group แรก ($1) ใช้จับคำแรก → ได้ 'John'
• \s = space ระหว่างคำ
• (\w+) = group ที่สอง ($2) ใช้จับคำที่สอง → ได้ 'Smith'
.
**Replacement Pattern '$2, $1' หมายความว่า:**
.
นำ $2 (นามสกุล) มาวางก่อน → ตามด้วย comma และช่องว่าง → จบด้วย $1 (ชื่อ)
.
ได้ผลลัพธ์เป็น 'Smith, John' ซึ่งเป็นรูปแบบที่นิยมใช้ในระบบฐานข้อมูลหรือรายการที่เรียงตามตัวอักษร
.
💡 **ส่วนตัวผมคิดว่า** capturing groups ($1, $2, $3) เป็นเทคนิคที่ทรงพลังมากครับ ช่วยให้เราจัดเรียงข้อมูลใหม่หรือสลับลำดับได้โดยไม่ต้องมานั่ง split แล้ว concat หลายครั้ง เซฟเวลาเยอะเลย 😎

### 4. ตัวอย่างที่ 4: Replace + Chain Methods ใน n8n Workflow (Real-World)

```excel
{{ $json.url.replace('http://', 'https://').replace(/\/api\/v1\//, '/api/v2/').trim() }}
```

**ผลลัพธ์:** `https://example.com/api/v2/users`

สมมติว่าข้อมูล URL ที่เข้ามามีค่า $json.url = 'http://example.com/api/v1/users ' (สังเกตว่ามีช่องว่างต่อท้าย)
.
**มาดูว่า Chained Methods ทำงานทีละขั้นตอน:**
.
**ขั้นตอนที่ 1:** .replace('http://', 'https://')
→ แปลง protocol จาก HTTP เป็น HTTPS เพื่อความปลอดภัย
→ ได้ 'https://example.com/api/v1/users '
.
**ขั้นตอนที่ 2:** .replace(/\/api\/v1\//, '/api/v2/')
→ อัพเกรด API version จาก v1 เป็น v2 โดยใช้ regex
→ ได้ 'https://example.com/api/v2/users '
→ ⚠️ สังเกต: ต้อง escape เครื่องหมาย / ด้วย \\ ใน regex เป็น \\/
.
**ขั้นตอนที่ 3:** .trim()
→ ตัดช่องว่างที่ท้ายข้อความออก
→ ได้ผลลัพธ์สุดท้าย 'https://example.com/api/v2/users'
.
🔧 **สถานการณ์จริงที่ผมเจอ:**
.
HTTP Request node รับ URL จาก previous node → ใช้ Set node กับ expression ชุดนี้แก้ไข URL → ส่ง URL ที่สะอาดแล้วไปเรียก API ต่อ
.
💡 **ส่วนตัวผมชอบ** method chaining แบบนี้มากครับ เพราะทำให้แปลงข้อมูลหลายขั้นตอนใน expression เดียวได้เลย ไม่ต้องสร้าง Set node หลายตัว workflow ดูกระชับและอ่านง่ายขึ้นเยอะ 😎

## หมายเหตุเพิ่มเติม

⚠️ **สิ่งที่ต้องรู้:** replace() เป็น JavaScript method มาตรฐาน ไม่ใช่ฟังก์ชันเฉพาะของ n8n แต่ใช้ได้ใน n8n expressions ทุก node ที่รองรับ expressions ครับ
.
💡 **Key Points ที่ต้องจำ:**
.
• แทนที่เฉพาะตัวแรกเท่านั้น (ใช้ replaceAll() ถ้าต้องการแทนที่ทั้งหมด)
• รองรับ regex patterns พร้อม flags (/g, /i, /m)
• รองรับ capturing groups ($1, $2) และ special patterns ($&, $`, $')
• สามารถ chain กับ methods อื่นได้ เช่น .replace().trim().toLowerCase()
• Return ค่าเป็น string ใหม่ (ไม่แก้ไข original string)
.
🔧 **Best Practices จากผม:**
.
**ใช้ Set node** สำหรับ simple replacements → ง่าย เห็นผลทันที ไม่ต้อง debug
.
**ใช้ Code node** สำหรับ complex replacements → ต้อง replace หลายขั้นตอนหรือมี logic ซับซ้อน
.
**Test regex ก่อนใช้งานจริง** → ใช้ regex101.com ช่วยทดสอบ จะได้ไม่ต้องมานั่งแก้ใน workflow แล้วเจอ error ทีหลัง 😅
.
**อย่าลืม escape special characters** → ใน regex ต้อง escape เครื่องหมายพิเศษ เช่น \\/ สำหรับ /, \\. สำหรับ . เป็นต้น
.
**ส่วนตัวผมแนะนำว่า** ถ้าต้องการแทนที่ทุกตัว ให้ใช้ replaceAll() ตรงๆ จะอ่านง่ายกว่า replace() กับ regex /g flag ครับ 😎

## คำถามที่พบบ่อย

**Q: replace() แทนที่เฉพาะตัวแรกจริงหรือ?**

ใช่ครับ ถูกต้อง 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 ดีกว่าครับ 😎

**Q: replace() กับ replaceAll() ต่างกันอย่างไร?**

นี่เป็นคำถามที่เจอบ่อยมากครับ 😅 มาดูความต่างกันแบบชัดๆ เลย:
.
**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 ทุกจุดในข้อความ

**Q: replace() รองรับ regex patterns หรือไม่?**

รองรับเต็มที่เลยครับ 😎 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

**Q: Capturing groups ($1, $2) ใน replace() ใช้ทำอะไรได้บ้าง?**

นี่เป็นฟีเจอร์ที่เจ๋งมากครับ 😎 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 สั้นลงและอ่านง่ายขึ้นเยอะเลย

**Q: ใช้ replace() ใน n8n workflow เมื่อไหร่?**

จากประสบการณ์ผม 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-all-n8n
- [split – ฟังก์ชันแยกข้อความจากตัวคั่น](https://www.thepexcel.com/functions/n8n/string-functions/split-n8n/)
- [trim – ลบ whitespace หน้าและหลังข้อความ](https://www.thepexcel.com/functions/n8n/string-functions/trim-n8n/)
- [substring – ฟังก์ชันดึงส่วนของข้อความตามตำแหน่ง](https://www.thepexcel.com/functions/n8n/string-functions/substring-n8n/)
- [toLowerCase – ฟังก์ชันแปลงเป็นตัวเล็ก](https://www.thepexcel.com/functions/n8n/string-functions/to-lower-case-n8n/)
- [includes – ฟังก์ชันตรวจสอบว่าอาร์เรย์มีค่าใดค่าหนึ่งหรือไม่](https://www.thepexcel.com/functions/n8n/string-functions/includes-n8n/)

## แหล่งข้อมูลเพิ่มเติม

- [n8n Expressions Documentation](https://docs.n8n.io/code/expressions/) _(documentation)_
- [n8n Built-in Data Transformation Functions](https://docs.n8n.io/code/builtin/data-transformation-functions/) _(documentation)_
- [MDN: String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) _(documentation)_

---

_Source: [https://www.thepexcel.com/functions/n8n/string-functions/replace-n8n/](https://www.thepexcel.com/functions/n8n/string-functions/replace-n8n/)_
