Regex Tester
Uji ekspresi reguler terhadap string dan lihat semua kecocokan yang disorot
Pola
String uji
Apa Itu Ekspresi Reguler?
Ekspresi reguler (regex atau regexp) adalah urutan karakter yang mendefinisikan pola pencarian. Regex tester memungkinkan kamu menulis pola, menjalankannya terhadap teks contoh, dan melihat semua kecocokan disorot secara langsung. Konsep ini bermula dari karya matematikawan Stephen Kleene tentang bahasa reguler pada 1950-an, dan Ken Thompson membangun mesin regex pertama ke dalam editor teks QED pada tahun 1968.
Mesin regex membaca pola dari kiri ke kanan, mengonsumsi karakter input saat mencoba mencocokkan. Mesin menggunakan backtracking ketika kecocokan parsial gagal: mesin mundur dan mencoba jalur alternatif melalui pola. Beberapa mesin (seperti RE2 yang digunakan di Go) menghindari backtracking sepenuhnya dengan mengonversi pola ke deterministic finite automata (DFA), yang menjamin pencocokan waktu linear dengan konsekuensi tidak mendukung fitur seperti back-reference.
Sintaks regex distandarisasi secara longgar. PCRE (Perl Compatible Regular Expressions) adalah varian yang paling umum, didukung oleh PHP, modul re Python, dan JavaScript dengan perbedaan kecil. POSIX mendefinisikan sintaks yang lebih terbatas yang digunakan oleh grep dan sed. Perbedaan ini penting saat memindahkan pola antar bahasa: lookahead yang bekerja di JavaScript mungkin tidak dapat dikompilasi sama sekali di mesin RE2 milik Go.
Mengapa Menggunakan Regex Tester Online?
Menulis regex dalam file kode berarti menyimpan, menjalankan, dan memeriksa output setiap kali kamu menyesuaikan pola. Regex tester berbasis browser menutup siklus umpan balik itu menjadi nol: kamu mengetik, kamu langsung melihat kecocokan.
Kasus Penggunaan Regex Tester
Referensi Cepat Sintaks Regex
Tabel di bawah mencakup token regex yang paling sering digunakan. Token-token ini bekerja di JavaScript, Python, Go, PHP, dan sebagian besar mesin yang kompatibel dengan PCRE. Ekstensi khusus bahasa (seperti conditional pattern Python atau named group JavaScript dengan sintaks \k) dicatat di bagian contoh kode.
| Pola | Nama | Deskripsi |
|---|---|---|
| . | Any character | Matches any single character except newline (unless s flag is set) |
| \d | Digit | Matches [0-9] |
| \w | Word character | Matches [a-zA-Z0-9_] |
| \s | Whitespace | Matches space, tab, newline, carriage return, form feed |
| \b | Word boundary | Matches the position between a word character and a non-word character |
| ^ | Start of string/line | Matches the start of the input; with m flag, matches start of each line |
| $ | End of string/line | Matches the end of the input; with m flag, matches end of each line |
| * | Zero or more | Matches the preceding token 0 or more times (greedy) |
| + | One or more | Matches the preceding token 1 or more times (greedy) |
| ? | Optional | Matches the preceding token 0 or 1 time |
| {n,m} | Quantifier range | Matches the preceding token between n and m times |
| () | Capturing group | Groups tokens and captures the matched text for back-references |
| (?:) | Non-capturing group | Groups tokens without capturing the matched text |
| (?=) | Positive lookahead | Matches a position followed by the given pattern, without consuming it |
| (?<=) | Positive lookbehind | Matches a position preceded by the given pattern, without consuming it |
| [abc] | Character class | Matches any one of the characters inside the brackets |
| [^abc] | Negated class | Matches any character not inside the brackets |
| | | Alternation | Matches the expression before or after the pipe |
Penjelasan Flag Regex
Flag (disebut juga modifier) mengubah cara mesin memproses pola. Di JavaScript, kamu menambahkannya setelah garis miring penutup: /pattern/gi. Di Python, kamu meneruskannya sebagai argumen kedua: re.findall(pattern, text, re.IGNORECASE | re.MULTILINE). Tidak semua flag tersedia di setiap bahasa.
| Flag | Nama | Perilaku |
|---|---|---|
| g | Global | Find all matches, not just the first one |
| i | Case-insensitive | Letters match both uppercase and lowercase |
| m | Multiline | ^ and $ match start/end of each line, not just the whole string |
| s | Dot-all | . matches newline characters as well |
| u | Unicode | Treat the pattern and subject as a Unicode string; enables \u{FFFF} syntax |
| y | Sticky | Matches only from the lastIndex position in the target string |
Contoh Kode
Contoh regex yang berfungsi dalam JavaScript, Python, Go, dan baris perintah. Setiap contoh menunjukkan konstruksi pola, ekstraksi kecocokan, dan output.
// Match all email addresses in a string
const text = 'Contact us at support@example.com or sales@example.com'
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g
const matches = text.matchAll(emailRegex)
for (const match of matches) {
console.log(match[0], 'at index', match.index)
}
// โ "support@example.com" at index 14
// โ "sales@example.com" at index 37
// Named capture groups (ES2018+)
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const result = '2026-03-30'.match(dateRegex)
console.log(result.groups)
// โ { year: "2026", month: "03", day: "30" }
// Replace with a callback
'hello world'.replace(/\b\w/g, c => c.toUpperCase())
// โ "Hello World"import re
# Find all IPv4 addresses
text = 'Server 192.168.1.1 responded, fallback to 10.0.0.255'
pattern = r'\b(?:\d{1,3}\.){3}\d{1,3}\b'
matches = re.findall(pattern, text)
print(matches) # โ ['192.168.1.1', '10.0.0.255']
# Named groups and match objects
date_pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
m = re.search(date_pattern, 'Released on 2026-03-30')
if m:
print(m.group('year')) # โ '2026'
print(m.group('month')) # โ '03'
# Compile for repeated use (faster in loops)
compiled = re.compile(r'\b[A-Z][a-z]+\b')
words = compiled.findall('Hello World Foo bar')
print(words) # โ ['Hello', 'World', 'Foo']package main
import (
"fmt"
"regexp"
)
func main() {
// Find all matches
re := regexp.MustCompile(`\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b`)
text := "Contact support@example.com or sales@example.com"
matches := re.FindAllString(text, -1)
fmt.Println(matches)
// โ [support@example.com sales@example.com]
// Named capture groups
dateRe := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
match := dateRe.FindStringSubmatch("2026-03-30")
for i, name := range dateRe.SubexpNames() {
if name != "" {
fmt.Printf("%s: %s\n", name, match[i])
}
}
// โ year: 2026
// โ month: 03
// โ day: 30
// Replace with a function
result := re.ReplaceAllStringFunc(text, func(s string) string {
return "[REDACTED]"
})
fmt.Println(result)
// โ Contact [REDACTED] or [REDACTED]
}# Find lines matching an IP address pattern
grep -E '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log
# Extract email addresses from a file
grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' contacts.txt
# Replace dates from YYYY-MM-DD to DD/MM/YYYY using sed
echo "2026-03-30" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/'
# โ 30/03/2026
# Count matches per file in a directory
grep -rcE 'TODO|FIXME|HACK' src/
# โ src/main.js:3
# โ src/utils.js:1