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

Is there a way to retrieve the default ObjectId generated by MongoDB when using NextAuth?

Is it possible to access the MongoDB ObjectId of the user currently logged in when using Next Auth with services like Twitter? When signing in, Next Auth creates a new user, account, and session, but I'm unable to retrieve the _id value for my server ...

What is the reason behind the failure of the cancel test?

I've created two test cases; one for testing the functionality of the Download button and the other for the Cancel button. However, I am encountering issues with the Cancel test failing consistently. Below is the code snippet where I'm attemptin ...

Since transitioning to the Eslint flat configuration, I have noticed that VSCode no longer displays any errors. However, Eslint continues to function properly in the

I have successfully implemented Eslint in a TypeScript Next.js project, but I am interested in transitioning to the new "flat config" approach offered by Eslint. To start, I have created a /eslint.config.js file that resembles this example: Section 1 Ho ...

How does handleChange receive the value as an input?

Greetings! Currently, I am delving into the world of React and JavaScript. I am experimenting with a Table Component demo that can be found at the following link: https://codesandbox.io/s/hier2?file=/demo.js:5301-5317 In the demo, there is a function defi ...

The double click functionality does not seem to be functioning within the Backbone View

I am trying to detect double click event within a Backbone view var HomePage = Backbone.View.extend({ initialize: function(){ this.render(); }, render: function(){ var template = _.template($('#app1').html()); ...

Applying a class to a specific element within another element using jQuery

This section pertains to a user account. When the user clicks on the edit button, I would like an edit box to appear within the same element as the button. Here is the HTML markup: <div class="col account"> <strong>Account Settings</st ...

Incorporating an external SVG link into a React application

While I may not be an SVG expert, I haven't encountered any issues with loading SVGs in my React app so far. I prefer using the svg tag over the image tag because sizing tends to present problems with the latter option when working with external links ...

Having trouble incorporating a variable into the Discord Client object in Typescript

As a newcomer to Typescript, I am working on creating a Discord bot using Typescript. I am trying to add a variable called "commands" to the Client object. In JavaScript, you can achieve this using the following code: Javascript const { Client } = require ...

The functionality of the Bootstrap4 accordion is not functioning according to my expectations

My goal is to create a unique e-commerce checkout page design. Each panel would be opened sequentially, with the next panel unfreezing when the action button of the current panel is clicked. However, I seem to be making a mistake as it is not working as in ...

Utilizing $stateParams within a personalized ui-router configuration attribute

Attempting to retrieve data from $stateParams within a custom ui-router state configuration property results in an empty object being logged. This outcome is anticipated given that I am straying from the standard ui-router configuration. Despite this devi ...

Guide on displaying several items from Laravel to Vue through the JavaScript filter method?

I need help transferring multiple objects from laravel to vue and filling my vue objects with information retrieved from the database. This is the data received from laravel: Vue objects to be populated: storeName: {}, storeUrl: {}, appName: {}, ...

Why does the error "Unable to resolve 'react-big-calendar/lib/sass/styles'" occur when using react-big-calendar with sass on Next.js?

When I tried to import 'react-big-calendar/lib/sass/styles' for my project using SASS, it resulted in an error stating: Module not found: Can't resolve 'react-big-calendar/lib/sass/styles' import { Calendar, momentLocalizer } from ...

Notification window when the page is being loaded

My goal is to have a module or alert box display when my website is opened, and then disappear after 5 minutes. To see an example of what I'm looking for, take a look at this website: On that website, there is a facebook.com box that automatically d ...

React: Issue with For Loop not recognizing updates in Hook's State

Recently, I successfully created a React application that displays each word of a sentence at a user-defined time interval for fast reading. However, I am now facing a challenge as I attempt to add a pause button functionality to the app. When I press the ...

`an error occurs when trying to loop through an object and add a key``

I'm currently facing an issue with adding a key to an object through looping. Here is the code snippet: function checkResult(want, reference) { const keys = Object.keys(reference); for(let i=0; i<Object.keys(reference).length; i++){ console ...

Prevent span/button clicks by graying them out and disabling the ability to click using HTML and JQuery

I am facing a challenge with my two spans that reveal specific information when clicked. I want to make one span appear as a disabled button (greyed out) when the other span is clicked. I tried using $("#toggle-odd").attr("disabled", tr ...

What is the most effective way to retrieve both grouped data and all data from mongodb?

I have successfully retrieved totalAccount and totalBalance using the code snippet above. However, I am facing an issue where no other field or data besides those two are showing up. How can I modify this code to also fetch all the data from my collectio ...

Using the reduce method in JavaScript or TypeScript to manipulate arrays

Just exploring my culture. I have grasped the concept of the reduce principle var sumAll = function(...nums: number[]):void{ var sum = nums.reduce((a, b) => a + b , 0); document.write("sum: " + sum + "<br/>"); } sumAll(1,2,3,4,5); The r ...

Exploring node.js: How to extract elements from a path

I have an array of individuals as shown below: individuals = ['personA', 'personB', 'personC']; I am looking to create a dynamic way to showcase each individual's page based on the URL. For instance, localhost:3000/indi ...

Disappearing Data: Struts2 Autocompleter's Entries Vanishing Upon Submit Button Click

I am currently utilizing the auto-complete feature from The issue I am facing is that when I type "Ball", the auto-complete suggests "Balloon". Even though both "Balloon" and "Balloon" appear in the text field and the list, when I click elsewhere on the s ...