Grab phone numbers and email from text

Hello I am trying to use the Java Script to grab all phone numbers and or emails from a description text. I can’t seem to get the function right any guidance for help here from the community?

function removePhoneNumbersAndEmails(text) {
  const phoneRegex = /(\+\d{1,2}\s?)?(\()?(\d{3})(?(2)\))[-.\s]?(\d{3})[-.\s]?(\d{4})\b/g;
  const emailRegex = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;

  // Remove phone numbers
  text = text.replace(phoneRegex, '');

  // Remove email addresses
  text = text.replace(emailRegex, '');

  return text.trim();

// Test the function with an example text
const text = `
  This is a sample text.
  You can contact me at +1 (123) 456-7890 or
  Please reach out to p1 for more information.

const result = removePhoneNumbersAndEmails(text);

My way of doing this is using a text prompt and OpenAI:

Given an input like this:


Can you return a JSON structure of all phone numbers and emails in the input?

A sample structure would look like this:

"phone": "+123456789,+123456790",
"email": ","

Please return just the JSON and nothing else.


Then you can query the JSON to return the final strings.