JavaScript String Search
JavaScript Search Methods
- String indexOf()
- String lastIndexOf()
- String startsWith()
- String endsWith()
JavaScript String indexOf()
The indexOf()
method returns the index of (the position of) the first
occurrence of a specified text in a string:
Example
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
Note:
- JavaScript counts positions from zero.
- 0 is the first position in a string, 1 is the second, 2 is the third, …
JavaScript String lastIndexOf()
The lastIndexOf()
method returns the index of the last occurrence of a specified text in a string:
Example
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");
Both indexOf()
, and lastIndexOf()
return -1 if the text is not found:
Example
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John");
Both methods accept a second parameter as the starting position for the search:
Example
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15);
The lastIndexOf()
methods searches backwards (from the end to the beginning), meaning: if the second parameter is 15
, the search starts at position 15, and searches to the beginning of the string.
Example
let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15);
JavaScript String search()
The search()
method searches a string for a specified value and returns the position of the match:
Example
let str = "Please locate where 'locate' occurs!";
str.search("locate");
Did You Notice?
The two methods, indexOf()
and search()
, are equal?
They accept the same arguments (parameters), and return the same value?
The two methods are NOT equal. These are the differences:
- The
search()
method cannot take a second start position argument. - The
indexOf()
method cannot take powerful search values (regular expressions).
You will learn more about regular expressions in a later chapter.
JavaScript String match()
The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.
Example 1
Search a string for “ain”:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
Example 2
Perform a global, case-insensitive search for “ain”:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
JavaScript String includes()
The includes()
method returns true if a string contains a specified value.
Example
let text = "Hello world, welcome to the universe.";
text.includes("world");
Syntax
string.includes(searchvalue, start)
searchvalue | Required. The string to search for |
start | Optional. Default 0. Position to start the search |
Returns: | Returns true if the string contains the value, otherwise false |
JS Version: | ES6 (2015) |
Check if a string includes “world”, starting the search at position 12:
let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
JavaScript String startsWith()
The startsWith()
method returns true
if a string begins with a specified value, otherwise false
:
Example
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
Syntax
string.startsWith(searchvalue, start)
Parameter Values
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
start | Optional. Default 0. The position to start the search. |
Examples
let text = "Hello world, welcome to the universe."; text.startsWith("world") // Returns false let text = "Hello world, welcome to the universe."; text.startsWith("world", 5) // Returns false let text = "Hello world, welcome to the universe."; text.startsWith("world", 6) // Returns true
Note: The startsWith()
method is case sensitive.
JavaScript String endsWith()
The endsWith()
method returns true
if a string ends with a specified value, otherwise false
:
Example
Check if a string ends with “Doe”:
let text = "John Doe";
text.endsWith("Doe");
Syntax
string.endsWith(searchvalue, length)
Parameter Values
Parameter | Description |
---|---|
searchvalue | Required. The value to search for. |
length | Optional. The length to search. |
Check if the 11 first characters of a string ends with “world”:
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11);
Note: The endsWith()
method is case sensitive.
levaquin pills order levaquin 500mg without prescription
dutasteride price celecoxib sale buy ondansetron 4mg
buy aldactone 100mg buy aldactone 100mg online cheap diflucan 200mg tablet
cheap ampicillin 500mg erythromycin buy online order erythromycin 250mg generic
cheap sildenafil order fildena online order robaxin 500mg generic
sildenafil 50mg cheap estrace 1mg generic buy estrace 2mg sale
cost lamotrigine 50mg purchase prazosin pills retin cream generic
buy tadalafil 10mg online cheap avana buy online order diclofenac 100mg without prescription
buy isotretinoin 40mg online cheap order zithromax 500mg without prescription buy azithromycin online cheap
indocin 50mg pill order lamisil 250mg online cheap order amoxicillin 500mg pill
order generic cialis buy cialis usa sildenafil otc
buy arimidex 1mg for sale order arimidex 1mg purchase sildenafil pill
cialis 20mg sans ordonnance cialis 20mg pas cher viagra 50mg generique pas cher
order deltasone 5mg generic viagra pills order generic sildenafil 50mg
tadalafil ohne rezept tadalafil 10mg kaufen ohne rezept sildenafil 100mg kaufen für männer
provigil 100mg us Cialis cheap price order diamox 250 mg pill
buy doxycycline 200mg for sale buy clomid 50mg sale order generic furosemide 100mg
order altace sale order avapro 300mg pill order astelin 10ml for sale
order clonidine for sale meclizine 25mg canada order spiriva pill
order buspar 5mg generic cheap ditropan 2.5mg order oxybutynin 2.5mg for sale
buy hytrin 5mg arava 20mg without prescription cost sulfasalazine 500 mg
fosamax online order order pepcid 40mg generic buy famotidine 40mg online
tacrolimus 1mg oral order tacrolimus 5mg without prescription urso order online
imdur 20mg cost buy micardis 20mg generic telmisartan 20mg usa
purchase molnunat order molnupiravir generic order prevacid 15mg pills
sertraline 50mg generic zoloft 50mg oral viagra 100mg cost
cost imuran 100 mcg cost salbutamol 100mcg viagra order
order cialis 40mg pills fluoxetine over the counter cheap viagra sale
cialis 5mg generic buy phenazopyridine 200mg generic buy symmetrel 100 mg generic
naltrexone 50 mg pill aripiprazole 20mg usa aripiprazole over the counter
purchase dapsone pills perindopril buy online perindopril pills
order provera 10mg without prescription medroxyprogesterone 5mg price order periactin 4 mg for sale
order modafinil 100mg online buy generic provigil ivermectin tablet 1mg
order generic luvox fluvoxamine 100mg over the counter buy glipizide pill
isotretinoin usa buy deltasone 10mg pills buy prednisone 5mg for sale
piracetam sale viagra 100mg brand order sildenafil 100mg
zithromax 500mg pill brand gabapentin 800mg order neurontin 100mg pill
cialis 10mg canada cialis mail order usa order sildenafil for sale
buy furosemide 40mg online cheap order furosemide 100mg without prescription plaquenil 200mg over the counter
buy tadalafil 20mg pill order tadalafil 20mg sale order anafranil
chloroquine for sale baricitinib for sale order olumiant generic
https://drugsoverthecounter.shop/# over the counter appetite suppressant
naproxen 500mg over the counter best over the counter nausea medicine
over the counter essentials ivermectin over the counter
https://over-the-counter-drug.com/# strongest over the counter painkillers
anthem over the counter catalogue over the counter allergy medicine
clobetasol cream over the counter over the counter ed meds
best over the counter medicine for sore throat best over the counter sleep aids
over the counter erectile pills at walgreens best over the counter allergy medicine
https://over-the-counter-drug.com/# over the counter testosterone
over the counter pain medication over the counter anti nausea
https://over-the-counter-drug.com/# best over the counter toenail fungus treatment
united healthcare over the counter essentials clobetasol cream over the counter
clobetasol cream over the counter over the counter birth control
nystatin cream over the counter over the counter pain meds for dogs
https://over-the-counter-drug.com/# over the counter bladder control
best appetite suppressant over the counter over the counter cold sore medicine
phentermine over the counter over the counter diuretics
https://over-the-counter-drug.com/# over the counter anti nausea
over the counter anti nausea medication naproxen 500mg over the counter
best over the counter medicine for sore throat over the counter medicine for acid reflux
where can you buy amoxicillin over the counter amoxil
https://zithromax.science/# zithromax coupon
zithromax z-pak zithromax z-pak price without insurance
buy minocycline 50mg otc generic stromectol
https://zithromax.science/# zithromax 500 mg for sale
amoxil for sale generic for amoxicillin
zithromax 500mg over the counter where can i get zithromax over the counter or buy cheap generic zithromax
http://majesticwindowcleaning.com/__media__/js/netsoltrademark.php?d=zithromax.science where can i buy zithromax uk
generic zithromax india can i buy zithromax online and zithromax 250 zithromax prescription online
zithromax 250 mg australia buy zithromax online
https://zithromax.science/# generic zithromax over the counter
stromectol for sale ivermectin cost
buy minocycline 50mg otc how to buy stromectol or Buy Online Ivermectin/Stromectol Now
http://kickashefilms.com/__media__/js/netsoltrademark.php?d=stromectol.science stromectol 3 mg tablet price
ivermectin rx ivermectin 3mg for lice and ivermectin syrup oral ivermectin cost
buy zithromax online australia buy zithromax
https://stromectol.science/# buy minocycline 50mg tablets
generic amoxil online buy amoxicillin online cheap
https://amoxil.science/# canadian pharmacy amoxicillin
doxylin doxycycline or doxycycline tetracycline
http://rmcgee.com/__media__/js/netsoltrademark.php?d=doxycycline.science how to buy doxycycline online
doxycycline 100mg dogs buy doxycycline for dogs and doxy 200 generic doxycycline
https://zithromax.science/# zithromax
cheap amoxil amoxicillin pharmacy price
buy doxycycline online doxy or odering doxycycline
http://leidengallery.net/__media__/js/netsoltrademark.php?d=doxycycline.science doxy 200
doxycycline monohydrate buy generic doxycycline and doxycycline doxycycline 500mg
price of amoxicillin without insurance amoxicillin 500mg for sale uk or amoxicillin 200 mg tablet
http://www.blue2can.net/__media__/js/netsoltrademark.php?d=amoxil.science amoxil generic
where can i buy amoxicillin over the counter uk amoxicillin tablets in india and amoxicillin pharmacy price amoxicillin 500 mg online
odering doxycycline doxylin
https://amoxil.science/# prescription for amoxicillin
stromectol for sale generic name for ivermectin
amoxicillin 500mg capsules buy amoxil
https://stromectol.science/# ivermectin 3mg pill
zithromax antibiotic zithromax online
https://amoxil.science/# amoxicillin online purchase
cheap amoxil order amoxicillin online uk
zithromax 500 tablet zithromax buy online no prescription or how to get zithromax over the counter
http://lupus.events/__media__/js/netsoltrademark.php?d=zithromax.science zithromax capsules 250mg
zithromax prescription zithromax 250 mg pill and where to get zithromax generic zithromax azithromycin
https://amoxil.science/# can i buy amoxicillin online
generic zithromax online paypal buy zithromax online fast shipping
https://amoxil.science/# amoxicillin discount coupon
amoxicillin 500mg for sale uk order amoxicillin uk
doxycycline pills buy doxycycline monohydrate or doxycycline hyclate
http://nationstarsloan.org/__media__/js/netsoltrademark.php?d=doxycycline.science buy doxycycline monohydrate
buy doxycycline 100mg doxycycline 100mg price and doxycycline buy doxycycline without prescription
ivermectin 3 mg cheap stromectol
https://stromectol.science/# ivermectin 9mg
order doxycycline online buy doxycycline without prescription uk
Drug information. earch our drug database.
stromectol south africa
Read information now. All trends of medicament.
п»їMedicament prescribing information. п»їMedicament prescribing information.
stromectol online pharmacy
Some trends of drugs. earch our drug database.
Everything what you want to know about pills. Definitive journal of drugs and therapeutics.
buy ivermectin nz
Drug information. All trends of medicament.
Some trends of drugs. Read here.
https://stromectolst.com/# ivermectin 80 mg
Medscape Drugs & Diseases. Best and news about drug.
minocycline 50mg tabs ivermectin goodrx or stromectol 3 mg tablets price
http://sleepingbeautywakes.org/__media__/js/netsoltrademark.php?d=stromectol.science minocycline 50 mg para que sirve
minocycline 100 mg online minocycline for rosacea and ivermectin 24 mg minocycline mr
Commonly Used Drugs Charts. Everything information about medication.
ivermectin 5
All trends of medicament. Generic Name.