Count the number of times a sub-string appears in a string

Опубликовано: 30 Апрель 2026
на канале: A Better Way to Learn JS
79
0

Welcome to our JavaScript tutorial on "Count the number of times a sub-string appears in a string in JS"! In this video, we dive deep into various methods of counting occurrences of specific characters or sub-strings within strings in JavaScript. This is an essential skill for any developer, as it allows for more dynamic and responsive web applications.

We start by exploring the split method to find the number of times the letter 'i' appears in the string 'Lorem ipsum dolor sit amet.' and how many times 'Hello' appears in 'Hello World'. These examples demonstrate how to leverage the length property of the array returned by split to count occurrences.

Next, we use the match method with a global regex pattern to directly retrieve an array of all 'i' occurrences in our first string, showcasing an alternative approach when dealing with regular expressions.

Following that, we employ a more manual method, iterating through each character of 'Hello wonderful world' to count the number of times 'l' appears. This technique is vital for understanding more granular control over string processing.

Lastly, we use the split, map, and filter methods to find the position of 'n' in the string 'Morning'. This combination of methods not only finds the occurrence but also demonstrates how to work with array indices and elements in JavaScript.

This tutorial is packed with practical, hands-on examples to help you understand and implement string manipulation techniques in your web projects. Whether you're a beginner looking to get a grasp on the basics or an experienced developer seeking to brush up on your skills, this video has something for everyone.

Don't forget to like, share, and subscribe for more helpful JavaScript tutorials!