Guide to finding and saving email addresses from a string output: extracting and storing each one individually in a text file

After collecting data from multiple sources, the output I obtained is as follows:

"addressId":"132234","businessEntryCount":2026},{"district":"Nordend-West","districtSlug":"frankfurt-am-main-nordend-west","addressId":"132232","businessEntryCount":1925}],"generated":"2022-01-23 19:35:43.469","grisuLocation":null,"district":null,"geo":null};
                kt.Data.SearchResult.distanceLocation = "Frankfurt am Main";
        kt.Data.SearchResult.distanceStreetnumber = "";
        kt.Service.citySlug = 'frankfurt';
        kt.Data.what = 'Handwerker';
        kt.Data.where = 'Frankfurt am Main';
        kt.Data.trade = 'Maler';
    

                {"@context":"http:\/\/schema.org","@type":"SearchResultsPage","mainEntity":{"@type":"ItemList","itemListElement":[{"@type":"ListItem","item":{"@type":"LocalBusiness","name":"Dachdecker Olaf Pocklitz","url":"http:\/\/www.test.de","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d29382e291d2970323331343338733938">[email protected]</a>","address":{"@type":"PostalAddress",...

The above information represents my collected data, and my goal is to extract all email addresses in order to store them in a file. In this instance, the email address would be [email protected]. If there are more than one email address, each additional email should be on a new line. I am facing difficulties in properly filtering out the email addresses and then saving them individually by line. Although I have managed to save the data, I am unsure of how to isolate just the email addresses:

console.log('received data: ' + data)
fs.writeFileSync('./results/test.json', data)

EDIT:

This is the code I have developed thus far:

var matches = data.match(/\"mainEntity":{"(.*?)\"}/);
    var preResult = [matches]
    
    //.itemListElement[0].item.email
console.log('received data: ' + preResult)
fs.writeFileSync('./results/test.json', preResult)

However, despite my efforts, I am unable to access the email within the result:

"mainEntity":{"@type":"ItemList","itemListElement":[{"@type":"ListItem","item":{"@type":"LocalBusiness","name":"Dachdecker Olaf test","url":"http:\/\/www.test.de","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbbaabcbb8fbbe2a0a1a3a6a1aae1abaa">[email protected]</a>","address":{....

Answer №1

Utilizing the selector:

element.mainEntity.itemListElement[0].item.email

const element =  {
  "@context":"http:\/\/schema.org",
  "@type":"SearchResultsPage",
  "mainEntity":{
    "@type":"ItemList",
    "itemListElement":[
      {
        "@type":"ListItem",
       "item":{
         "@type":"LocalBusiness",
         "name":"Dachdecker Olaf Pocklitz",
         "url":"http:\/\/www.test.de",
         "email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94e0f1e7e0d4e0b9fbfaf8fdfaf1baf0f1">[email protected]</a>",
         "address": ""
       }
      }
    ]
  }}

console.log(element.mainEntity.itemListElement[0].item.email)
                                                         

Update

d = {"@context":"http:\/\/schema.org","@type":"SearchResultsPage","mainEntity":{"@type":"ItemList","itemListElement":[{"@type":"ListItem","item":{"@type":"LocalBusiness","name":"Dachdecker Olaf Pocklitz","url":"http:\/\/www.pocklitz.de","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="214e514e424a4d48555b61550c4e4f4d484f440f4544">[email protected]</a>","address":{"@type":"PostalAddress","postalCode":"65931","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Erfurter Weg 21"},"telephone":"(069) 765820","aggregateRating":{"@type":"AggregateRating","worstRating":1,"bestRating":5,"ratingValue":1,"reviewCount":3,"itemReviewed":{"@type":"Organization","name":"Dachdecker Olaf Pocklitz"}}}},{"@type":"ListItem","item":{"@type":"LocalBusiness","name":"Dachdeckerei Havan","url":"http:\/\/www.dachdeckereihavan.de","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="452c2b232a052124262d2120262e2037202c2d2433242b6b2120">[email protected]</a>","address":{"@type":"PostalAddress","postalCode":"60599","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Offenbacher Landstr. 364"},"telephone":"(069) 651540"}},{
...
console.log( d.mainEntity.itemListElement[0].item.email)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automatically conceal a div or flash message after a short period of time by utilizing CSS3 in a React

I am relatively new to the React environment and recently created a basic component for a bookmarking feature. Essentially, when the favourite icon is clicked, an ajax call is sent > a record is created in the database > on ajax success, a flash me ...

Parent window encountering issue while calling child window function

Recently, I have been encountering issues with using an iframe inside a thickbox and calling the function within the iframe. The code snippet I'm currently using is window.frames[0].test();, which seems to be working inconsistently across different br ...

The JavaScript function Date().timeIntervalSince1970 allows you to retrieve the time

For my React Native app, I currently set the date like this: new Date().getTime() For my Swift App, I use: Date().timeIntervalSince1970 Is there a JavaScript equivalent to Date().timeIntervalSince1970, or vice versa (as the data is stored in Firebase clo ...

Using dialogue boxes instead of alert and prompt functions in jQuery can enhance user interaction and

I need assistance with implementing a dialog feature in fullcalendar. Currently, I am only able to trigger an alert or prompt when clicking on an event. What is the appropriate JavaScript code to display a dialog? Here is my jQuery code: $(document).read ...

Making an Ajax request with JSON is yielding unexpected variables that cannot be modified or removed

Attempting to make an AJAX call using a script: $.ajax({ url: pageURL, data: loadData, type: 'POST', cache: false, dataType: 'json', success: function (data) { //if the call was successful console.log(su ...

NextJS makes it simple to link to an external site by using the built-in

I find myself stuck on what seems like a simple question, but I can't figure it out. How do I accomplish this? I've searched in several different ways for an answer to this question but haven't been able to find a clear solution. I'm w ...

Determine the total hours and minutes elapsed between two specific dates and times

Looking for some assistance here. I have a form where users need to input a start time and end time of an incident. After entering this information, they would manually calculate the duration between the two date times. I am attempting to streamline this p ...

React-highlightjs failing to highlight syntax code properly

I'm currently using the react-highlight library to highlight code snippets in my next.js application. However, I've encountered an issue with the code highlighting when using the a11y-dark theme. https://i.stack.imgur.com/ip6Ia.png Upon visitin ...

The event "subscriptionRemoved" is not being triggered when a password change is made on the Microsoft Graph API

Utilizing the Microsoft Graph API, I have set up subscriptions to receive notifications for calendar events through Node.js. As per the guidelines outlined in the documentation on Enhancing notification reliability for Outlook resources (preview), it speci ...

The issue occurs when using NextJS in conjunction with React-Query, where the query disappears from the React-Query DevTools after clicking on the link for a

Using the react-query hook export const useSearchClient: UseSearchClient = (filters) => { return useQuery({ queryKey: ['clients'], queryFn: queryFn(filters), enabled: false, }) } Implementing the client component const { data, ...

React hooks - Issue with updating state properties within sidebar display

When resizing the window, I have an event that will display either the desktop sidebar or the mobile sidebar. However, there are variables that are not immediately updated to show the sidebar correctly based on the window size. In a desktop window, this ca ...

What is the best way to distribute my rabbitMQ code among different components?

I am looking for a way to optimize my rabbitMQ connection code by creating it once and using it across multiple components. Currently, every time I need to pass data to my exchange and queue, I end up opening and closing the connection and channel multiple ...

Customizing the DatePicker with a unique button in material-ui

For my current project, I am utilizing a Datepicker component. I am looking to incorporate a custom information button in the upper right corner of the calendar layout, similar to the example image provided below: https://i.stack.imgur.com/fHMbn.png Unfo ...

NodeJS - Issue: The procedure specified could not be located in the bcrypt_lib.node

After upgrading from Node.js 0.12.7 to 4.2.1, I encountered an error when attempting to start my server: $ node server.js C:\Users\me\documents\github\angular-express-auth\node_modules\bcrypt\node_modules\bindi ...

Leverage Jasmine for testing a jQuery plugin

I'm currently utilizing the angular-minicolors library () within an angular controller: angular.element("myelement").minicolors({ position: 'top left', change: function() { //custom code to run upon color change } }) Wh ...

Enhancing the efficiency of JavaScript code

Imagine you have a web application processing 1,000,000 user logins per hour. and the code below is executed during each user login: if (DevMode) { // make an Ajax call } else if (RealMode) { // make another Ajax call } else { // Do something ...

Merge two separate mongodb records

Just getting started with javascript / express / mongodb. For my initial project, I am working on a simple task manager where todos are categorized by priority - "high," "mid," or "low." However, I am facing an issue when trying to display different entri ...

Obtain the title of the text generated by clicking the button using a script function

I am working on a piece of code that generates a text box within a button's onclick function. My goal is to retrieve the name value of each text box using PHP. <script language="javascript"> var i = 1; function changeIt() ...

Looking to install nodemon for Node.js on macOS? If you're encountering a "command not found" error, here's

After installing nodemon using the command npm install -g nodemon, I encountered a Permissions issue. To resolve this, I used the sudo npm install -g nodemon command. However, when attempting to run the "nodeman" command, I kept receiving an error that s ...

Using JavaScript's regular expressions to identify a code block that commences with a specified pattern

Currently, I am working on a JavaScript script and I am in need of a Regex pattern to quickly match "JSDocs". The specific pattern that I am trying to match looks like this: # this is block1 /// text /// text /// text /// text # this is block2 /// text // ...