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 var secureQueryRegex = /(
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.
Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.