RegExp Tasdiqlar (Assertions)
Tasdiqlar — chegaralar (boundaries) va ko'rib chiqishlar (lookarounds) bilan moslikni tekshiradi:
- Matn chegaralari va so'z chegaralari.
- Ko'rib chiqishlar: Lookahead va Lookbehind.
// Matn boshiga moslik
const pattern = /^W3Schools/;
// Matn oxiriga moslik
const pattern2 = /W3Schools$/;Tasdiqlar jadvali
| Sintaksis | Nomi | Tavsif |
|---|---|---|
^ | Matn chegarasi | Matn boshiga mos keladi. |
$ | Matn chegarasi | Matn oxiriga mos keladi. |
\b | So'z chegarasi | So'z boshi yoki oxiriga mos keladi. |
\B | So'z chegarasi | So'z boshi yoki oxiriga mos kelmaydigan joyga mos keladi. |
(?=...) | Lookahead | Keyingi matnga mos keladi. |
(?!...) | Salbiy Lookahead | Keyingi matnga mos kelmasa moslik beradi. |
(?<=...) | Lookbehind | Oldingi matnga mos keladi. |
(?<!...) | Salbiy Lookbehind | Oldingi matnga mos kelmasa moslik beradi. |
^ metabelgisi
^ metabelgisi matnning boshiga mos keladi.
const pattern = /^W3Schools/;
let text = "W3Schools Tutorial";
let result = pattern.test(text); // trueconst pattern = /^W3Schools/;
let text = "Hello W3Schools";
let result = pattern.test(text); // false$ metabelgisi
$ metabelgisi matnning oxiriga mos keladi.
const pattern = /W3Schools$/;
let text = "Hello W3Schools";
let result = pattern.test(text); // trueconst pattern = /W3Schools$/;
let text = "W3Schools tutorial";
let result = pattern.test(text); // false\b metabelgisi (so'z chegarasi)
\b metabelgisi so'zning boshi yoki oxiriga mos keladi.
Misollar
So'z boshida "LO" belgilarini qidirish:
let text = "HELLO, LOOK AT YOU!";
let result = text.search(/\bLO/);So'z oxirida "LO" belgilarini qidirish:
let text = "HELLO, LOOK AT YOU!";
let result = text.search(/LO\b/);Lookahead x(?=y)
x(?=y) — "x" ga faqat "y" undan keyin kelsa mos keladi.
let text = "W3Schools Tutorials";
let pattern = /W3Schools(?= Tutorials)/;
let result = pattern.test(text); // trueSalbiy Lookahead x(?!y)
x(?!y) — "x" ga faqat "y" undan keyin kelmasa mos keladi.
let text = "W3Schools Tutorials";
let pattern = /W3Schools(?! Tutorials)/;
let result = pattern.test(text); // falseTushuntirish:
W3Schools— so'zma-so'z moslik.(?! Tutorials)— keyingi qism " Tutorials" emasligini tasdiqlaydi.- Agar text = "W3Schools Tutorials" bo'lsa, test
falseqaytaradi. - Agar text = "W3Schools Website" bo'lsa, test
trueqaytaradi.
Lookbehind (?<=y)x
(?<=y)x — "x" ga faqat "y" undan oldin kelsa mos keladi.
let text = "Hello W3Schools";
let pattern = /(?<=Hello )W3Schools/;
let result = pattern.test(text); // trueSalbiy Lookbehind (?<!y)x
(?<!y)x — "x" ga faqat "y" undan oldin kelmasa mos keladi.
let text = "Hello W3Schools";
let pattern = /(?<!Hello )W3Schools/;
let result = pattern.test(text); // falseMuntazam ifoda guruhlari
| Belgi | Tavsif |
|---|---|
(x) | x ga mos keladi va uni saqlaydi. |
(?<n>x) | x ga mos keladi va uni n nomi bilan belgilaydi. |
(?flag:x) | Faqat x uchun flag(lar)ni yoqadi. |
(?flag-flag:x) | Faqat x uchun flag(lar)ni o'chiradi. |
Saqlash guruhlari (x)
let text = "Haha, haha, haha.";
const pattern = /(haha)+/;
let result = text.match(pattern);Tushuntirish:
(haha)— belgilar guruhini saqlaydi.(haha)+— guruhning nol yoki undan ko'p marta takrorlanishiga mos keladi.text.match()— natijalar massivini qaytaradi.
Nomlangan saqlash guruhlari (?<n>)
const text = "Name: John Doe";
// Nomlangan saqlash guruhlarini ishlatish
const regex = /(?<firstName>\w+) (?<lastName>\w+)/;
const match = text.match(regex);
let fName = match.groups.firstName;
let lName = match.groups.lastName;Tushuntirish:
(?<firstName>\w+)— so'zni saqlaydi vafirstNamedeb nomlaydi.(?<lastName>\w+)— so'zni saqlaydi valastNamedeb nomlaydi.text.match()—groupsxususiyatiga ega massiv qaytaradi.match.groups—{firstName: "John", lastName: "Doe"}obyektini qaytaradi.
Saqlash guruhlarini ishlatganda, String.match() va RegExp.exec() metodlari groups xususiyatiga ega moslik obyektini qaytaradi. Bu xususiyat guruhlarning nomlari va qiymatlarini saqlaydi.
Guruh modifikatorlari (?flag)
(?flag) sintaksisi — bu guruh modifikatori. U flaglarni shablonning bir qismiga qo'llash imkonini beradi.
let text = "W3Schools tutorials.";
const pattern = /(?i:W3Schools) tutorials/;
// true qaytaradi:
let result = pattern.test(text);let text = "W3Schools Tutorials.";
const pattern = /(?i:W3Schools) tutorials/;
// false qaytaradi (tutorials katta harf bilan):
let result = pattern.test(text);Muntazam ifoda metodlari
String Metodlari
| Metod | Tavsif |
|---|---|
match(regex) | Natijalar massivini qaytaradi. |
matchAll(regex) | Natijalar iteratorini qaytaradi. |
replace(regex) | Yangi satr qaytaradi. |
replaceAll(regex) | Yangi satr qaytaradi. |
search(regex) | Birinchi moslik indeksini qaytaradi. |
split(regex) | Natijalar massivini qaytaradi. |
RegExp Metodlari
| Metod | Tavsif |
|---|---|
regex.exec() | Natijalar iteratorini qaytaradi. |
regex.test() | true yoki false qaytaradi. |
