Cara kerja pola
| Bagian | Arti |
|---|---|
^ | Awal string |
\d+ | Satu atau lebih digit 0–9 |
$ | Akhir string |
Contoh kode
JavaScript
const re = /^\d+$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'\d+', value))PHP
preg_match('/^\d+$/', $value) === 1;
Catatan dan batasan
Gunakan ^\d*$ untuk juga mengizinkan string kosong. Di JavaScript, \d hanya cocok dengan digit ASCII; di Python 3 juga cocok dengan digit dari skrip lain (seperti ٣) kecuali kamu memberi re.ASCII.
Regex Tester
Ketik pola dan lihat hasil cocok langsung ter-highlight di teks ujimu.