jQuery Get All Occurrences of a String inside a String

Sam Deering
Sam Deering
Share

In this example you can get all occurrences of an email address on the web page. Useful if you have to change the email address for a specific page or sub section of your site. Might be useful to screen scrape email addresses off web pages too, although that’s not what i am trying to achieve here. See more jQuery .each Examples. i = insensitive (case) g= global search m = match over multiple lines

var iframeSrc = 'test.html?param=value&email=noobs@noob.com&moreparams=values';
var emailRegex= /[._a-zA-Z0-9-]+@[._a-zA-Z0-9-]+/igm;

console.log(iframeSrc);
console.log(emailRegex.test(iframeSrc));
console.log(iframeSrc.match(emailRegex));

$.each(iframeSrc.match(emailRegex), function(index, value) { 
     console.log(index + ". " + value);
});
//output: 0. noobs@noob.com
This find all html between the tags:
var secureQueryRegex = /(
)/igm; /* loop for each query found in data */ $.each(data.match(secureQueryRegex), function(index, value) { console.log(index + ". " + value); }); //example output: //0. query=find all shops //1. query=find all locations
//etc...

Frequently Asked Questions (FAQs) about Finding Occurrences of a Substring in a String

How can I find all occurrences of a substring in a string using jQuery?

jQuery does not have a built-in function to find all occurrences of a substring in a string. However, you can use JavaScript’s match() function in combination with a global regular expression. Here’s an example:

var string = "Hello, world! Hello, user!";
var substring = "Hello";
var occurrences = string.match(new RegExp(substring, 'g'));

In this code, ‘g’ is a flag that indicates a global search, meaning it will find all matches, not just the first one.

Can I use the match() function to find occurrences of a substring in a string in Python?

In Python, you can use the count() function to find all occurrences of a substring in a string. Here’s an example:

string = "Hello, world! Hello, user!"
substring = "Hello"
occurrences = string.count(substring)

In this code, the count() function returns the number of occurrences of the substring in the string.

How can I find all occurrences of a substring in a list of strings in Python?

You can use a combination of the count() function and a for loop to find all occurrences of a substring in a list of strings. Here’s an example:

list_of_strings = ["Hello, world!", "Hello, user!", "Goodbye, world!"]
substring = "Hello"
occurrences = [string.count(substring) for string in list_of_strings]

In this code, the for loop iterates over each string in the list, and the count() function finds the number of occurrences of the substring in each string.

Can I use the match() function to find occurrences of a substring in a string in JavaScript?

Yes, you can use the match() function in JavaScript to find all occurrences of a substring in a string. However, you need to use it in combination with a global regular expression, as shown in the answer to question 1.

How can I find the positions of all occurrences of a substring in a string in JavaScript?

You can use a while loop in combination with the indexOf() function to find the positions of all occurrences of a substring in a string. Here’s an example:

var string = "Hello, world! Hello, user!";
var substring = "Hello";
var positions = [];
var position = string.indexOf(substring);

while (position != -1) {
positions.push(position);
position = string.indexOf(substring, position + 1);
}

In this code, the while loop continues as long as the indexOf() function returns a position, which it does not when it can no longer find the substring. The positions of the occurrences are stored in the positions array.