DEV Community

Cover image for String search methods in JavaScript.!
Hodiev
Hodiev

Posted on • Updated on

String search methods in JavaScript.!

String indexOf()
String lastIndexOf()
String search()
String match()
String matchAll()
String includes()
String startsWith()
String endsWith()

indexOf()
Ushbu metod string satrining elementiga mos indeks raqamni qaytaradi, element topilmasa -1 qaytaradi!

let myEmail = "samandarhodiev04@gmail.com";
console.log(myEmail);
//natija - samandarhodiev04@gmail.com 

let indexOf_ = myEmail.indexOf('a');
console.log(indexOf_);
//natija - 1
Enter fullscreen mode Exit fullscreen mode

LastIndexOf()
Ushbu metod IndeOF() metodi kabi ishlaydi ammo ular orasida bir farq bor.

Farq:
indexOf() - metodi izlanayotgan satr elementiga mos indeks raqamini chapdan o'ngga qarab izlaydi va topilgan 1-natijani qaytaradi.
LastIndexOf() - metodi esa o'ngdan chapga qarab izlaydi va topilgan 1-natijani qaytaradi.!

let myEmail = "samandarhodiev04@gmail.com";
console.log(myEmail);
//natija - samandarhodiev04@gmail.com 

let LastIndexOf_ = myEmail.lastIndexOf('a');
console.log(LastIndexOf_);
//natija - 19
Enter fullscreen mode Exit fullscreen mode

search()
Ushbu metod satrni yoki satr elementini oladi va mos keluvchi indeks raqamini qaytaradi.!
IndexOf() va Search() metodiga o'xshash bo'lishiga qaramasdan, search() elementga mos indeks raqam qayerdan boshlab izlanishi kerakligini belgilayolmaydi.!

let myEmail = "samandarhodiev04@gmail.com";
console.log(myEmail);
//natija - samandarhodiev04@gmail.com 

let search_ = myEmail.search('.com');
console.log(search_);
//natija - 22
Enter fullscreen mode Exit fullscreen mode

match()
Ushbu metod izlanayotgan element yoki satr ni o'z ichiga olgan massivni qaytaradi, ushbu metod katta-kchik harflarga sezgir va quyidagi misolda katta kchik harflar farq qilmagan holda izlash imkonini beruvchi xolatlar keltirilgan.!

let fruits_ = 'apple, banana, lEmon, apple, mango, appLe';
console.log(fruits_);
//natija - apple, banana, lemon, apple, mango, apple

let match_1 = fruits_.match('le');
console.log(match_1);
//natija - ['le', index: 3, input: 'apple, banana, lemon, apple, mango, apple', groups: undefined]

let match_2 = fruits_.match(/le/g);
console.log(match_2);
//natija - ['le', 'le']

let match_3 = fruits_.match(/le/gi);
console.log(match_3);
//natija - ['le', 'lE', 'le', 'Le']
Enter fullscreen mode Exit fullscreen mode

matchAll()
ES2020

includes()
Ushbu metod string satiri ichida biror bir satr yoki element bor yoki yo'qligini aniqlabberadi, bor bo'lsa - true yo'q bo'lsa - false qiymat qaytaradi, ushbu metod ES6 da kiritilgan va katta-kichik xarflarga sezgir.!

let fruits_ = 'apple, banana, lEmon, apple, mango, appLe';
console.log(fruits_);
//natija - apple, banana, lemon, apple, mango, apple

let includes_1= fruits_.includes('apple');
console.log(includes_1);
//natija - true

let includes_2= fruits_.includes('carrot');
console.log(includes_2);
//natija - false
Enter fullscreen mode Exit fullscreen mode

startWith()
ES6.
Ushbu metod string elementi qanday xarf yoki raqam bilan boshlanishiga qarab true yoki false qiymat qaytaradi shuningdek katta va kichik xarflarga sezgir va izlash qayerdan boshlanishi kerakligini belgilash mumkin.
sintaksis: startwith('sign' , n)!

let myEmail = "samandarhodiev04@gmail.com";
console.log(myEmail);
//natija - samandarhodiev04@gmail.com 

let startWith_1 = myEmail.startsWith('s');
console.log(startWith_1);
//natija - true

let startWith_2 = myEmail.startsWith('M');
console.log(startWith_2);
//natija - false

let startWith_3 = myEmail.startsWith('a', 12);
console.log(startWith_3);
//natija - false
Enter fullscreen mode Exit fullscreen mode

EndsWith()
ES6.
Ushbu metod string elementi qanday xarf yoki raqam bilan boshlanishiga qarab true yoki false qiymat qaytaradi shuningdek katta va kichik xarflarga sezgir va izlash qayerdan boshlanishi kerakligini belgilash mumkin.
sintaksis: startwith('sign' , n)!

let myEmail = "samandarhodiev04@gmail.com";
console.log(myEmail);
//natija - samandarhodiev04@gmail.com 


let endsWith_1 = myEmail.endsWith('M');
console.log(endsWith_1);
//natija - false

let endsWith_2 = myEmail.endsWith('m');
console.log(endsWith_2);
//natija - true

let endsWith_3 = myEmail.endsWith('o',25);
console.log(endsWith_3);
//natija - true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)