Article

Finding emails in a div

https://cdn.jsdelivr.net/npm/[email protected]/dist/js.cookie.min.js
Copy to clipboard

The following code will find all email addresses in our text div on the right: https://raw.githubusercontent.com/finsweet/jqueryCourse/main/jsobjects/ex3.js?actionable=true

In the above code reg_pattern.exec() does a search on the example_string variable and stores its output in the result variable.

In this case the value of result is c.

We can also replace the patterned matched with an empty string with this line example_string.replace(reg_pattern, "")

We can write more complex regex to match sophisticated patterns.

Examples of complex regex include:

[^a-zA-Z] : Matches any string that does not contain characters ranging from a through z and A through Z.

[a-zA-Z0-9]* Matches all words in text. ^chapter : Matches any string which starts with chapter at the beginning of it.

/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)/ : Matches any kind of email addresses.

<script>
$(document).ready(function(){
  XT1
});
</script>
Copy to clipboard
Send to Live Demo

Live demo

<div id="minidivpage" class="example-live"><div id="textblock">Lorem [email protected] ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim [email protected] veniam, quis nostrud exercitation ullamco laboris nisi [email protected] ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu [email protected] fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div></div>

<script>    
$(document).ready(function() {
  XT1
});
</script>
<script>
  
</script>
Copy to clipboard
Press the Run Demo button to execute the scripts above.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Your browser is too small

Resize your browser to be at least 769px wide.