Skip to content

Instantly share code, notes, and snippets.

@denzndhauz
Last active January 22, 2020 08:52
Show Gist options
  • Save denzndhauz/b1de7a909827312fc9a6c8da6f1c6d92 to your computer and use it in GitHub Desktop.
Save denzndhauz/b1de7a909827312fc9a6c8da6f1c6d92 to your computer and use it in GitHub Desktop.
Scan Phone Number and Email in a line/sentence in a string. If detected in a certain line/sentence, remove the whole line/sentence.
/*
* Author: Edwin Julaton
* Last Update: Jan. 22, 2020
* Description: Scan Phone Number and Email in a line/sentence in a string. If detected in a certain line/sentence, remove the whole line/sentence.
*/
var sample_text = `Hello,
We have a client in CA Beach looking for a 50% time Provider.. This is a great opportunity to work close to the beach, have a job with great hours, have three day weekends, and enjoy an area that has a lot to offer. I've provided a few facts below. If you would like more information, please email your updated CV to foo.bat@example.com, and/or call/text 999-999-9999.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer a laoreet orci. Praesent id tempor urna. Sed volutpat nisi sit amet hendrerit malesuada. Quisque interdum dui leo, eu volutpat neque fringilla id. Phasellus bibendum lacus ac vehicula venenatis. Duis quam velit, feugiat id iaculis semper, eleifend malesuada quam. Sed eu vestibulum odio, id hendrerit velit. Integer tempus eros in dictum ultricies. Donec fermentum ligula id sem pellentesque, eget tempor magna sagittis. Morbi id leo sem. Nullam nec egestas erat, at semper sem.
In placerat arcu non ultricies facilisis. Fusce et purus dolor. Aliquam eu venenatis ante. Donec quis lacus purus. In sed sagittis eros. Quisque ac tempor enim. Proin a libero vitae orci facilisis luctus. Vivamus nulla odio, tempor ac magna at, posuere varius libero. Praesent rutrum, sem at luctus pharetra, tortor tortor consequat tellus, id fringilla ante lectus vel magna. Aenean viverra lectus vitae nunc commodo blandit. Donec condimentum ex quis magna tristique pharetra. Curabitur in elit neque.
Vestibulum vel mauris ac tellus iaculis pulvinar in et lorem. Integer consectetur mi sit amet mollis mattis. Aliquam varius viverra faucibus. Donec id tellus quis est fringilla interdum. Mauris a lectus consequat, consectetur mi vitae, ultrices felis. Ut in enim in nisl hendrerit condimentum non vitae eros. Vestibulum quis lectus sem. Donec semper elit enim, et sollicitudin mi posuere id. Sed ac pharetra sapien. Ut fermentum lectus vehicula nunc tempor convallis. Suspendisse volutpat ipsum ac eros facilisis convallis. Vestibulum sodales dolor eget dolor blandit, a rhoncus tortor tristique. Aenean molestie ultricies pharetra. Sed blandit quam eget aliquam molestie. In hac habitasse platea dictumst. Cras iaculis aliquam hendrerit.
Nam porttitor diam ac nisl pharetra ornare. Aliquam a sagittis elit. Duis vitae lorem elementum, posuere odio nec, dictum risus. Aliquam lacus eros, mattis nec consectetur sed, placerat eu massa. Nulla blandit, urna eu venenatis aliquam, leo quam auctor ex, et pharetra nisi lacus in lacus. Suspendisse potenti. Morbi tempus purus et tortor luctus, nec aliquet lectus mattis. Vivamus scelerisque ipsum sed ipsum tempus, at tempor dui dignissim. Proin in tincidunt eros. Nulla commodo orci dolor. Sed gravida varius ultricies. Nullam a maximus metus, vitae elementum ante.
Donec ipsum ex, laoreet tempor sem sed, tincidunt consectetur massa. Integer euismod, magna et tincidunt consectetur, purus nunc bibendum odio, in cursus urna eros eget leo. Sed convallis ligula ac elit faucibus tempus. Suspendisse at tristique nibh. Integer et iaculis lectus, in laoreet libero. Phasellus at porta magna. Proin tristique nec ex id pellentesque. Suspendisse vestibulum augue vel efficitur tristique. Maecenas ac rhoncus ipsum, vel laoreet velit. Nullam sodales porta ipsum, ut pellentesque tellus pellentesque sed.
Foo Bar
Senior Recruiter
Data Provider
P.O. Box 9999
Watkinsville, GA 30699
Office: 999-999-9999
Direct: 999-999-9999
Cell: 999-999-9999
Fax: 999-999-9999
Email: foo.bar@example.com`;
// Copy Original Text
var test_str = sample_text;
function is_correct_sentence(input_str) {
var first_char = input_str[0];
var last_char = input_str[input_str.length - 1];
return /[A-Z]/.test(first_char) && last_char == "."
}
function extract_emails(input_str) {
return input_str.match(/(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/g);
}
function extract_phone_numbers(input_str) {
return (libphonenumber).findNumbers(input_str, 'US', {v2: true});
}
var t0 = performance.now();
/*
* Change email format dot to literal `(dot)` word.
* It will be useful to extract the sentences easily.
*/
// Extract emails to convert
var emails = extract_emails(test_str);
if(emails && Array.isArray(emails) && emails.length > 0) {
emails.forEach(function(email) {
var new_email_format = email.replace(/\./g, '(dot)');
test_str = test_str.replace(email, new_email_format);
});
}
// Reformat numbers with FORMAT: 999.999.9999 TO: 999-999-9999 so that it can't be detected as sentence.
var detected_phone_numbers = extract_phone_numbers(desc);
detected_phone_numbers.forEach(function(phone) {
var raw_extracted_phone_number = test_str.substring(phone.startsAt, phone.endsAt);
if(raw_extracted_phone_number.indexOf('.') != -1) {
var new_formatted_phone_number = raw_extracted_phone_number.replace(/\./g, '-');
test_str = test_str.replace(raw_extracted_phone_number, new_formatted_phone_number);
}
});
// Split strings using newline code.
string_splitted_via_newline = test_str.split('\n');
string_splitted_via_newline.forEach(function(value) {
// extract all splitted strings to sentences
var sentences = value.match(/[^.?!]+[.!?]+[\])'"`’”]*/g);
if(sentences != null && Array.isArray(sentences)) {
sentences.forEach(function(sentence) {
// Remove whitespaces in a sentence.
sentence = sentence.trim();
var is_found = false;
if(is_correct_sentence(sentence)) {
var captured_numbers = extract_phone_numbers(sentence);
if(captured_numbers && captured_numbers.length > 0) {
is_found = true;
} else {
// Convert `(dot)` format to `.`
var parse_emails_in_sentence = sentence.replace('(dot)', '.');
var found_emails = extract_emails(parse_emails_in_sentence);
if(found_emails != null && found_emails.length > 0) {
is_found = true;
}
}
if(is_found) {
if(test_str.indexOf('\n' + sentence) != -1) {
test_str = test_str.replace('\n' + sentence, '');
} else {
test_str = test_str.replace(sentence, '');
}
}
}
});
} else if(value != '') {
var captured_numbers = extract_phone_numbers(value);
var is_found = false;
if(captured_numbers && captured_numbers.length > 0) {
is_found = true;
} else {
// Convert `(dot)` format to `.`
var parse_emails_in_a_line = value.replace(/\(dot\)/g, '.');
var found_emails = extract_emails(parse_emails_in_a_line);
if(found_emails != null && found_emails.length > 0) {
is_found = true;
}
}
if(is_found) {
if(test_str.indexOf(value + '\n') != -1) {
test_str = test_str.replace(value + '\n', '');
} else if(test_str.indexOf('\n' + value) != -1) {
test_str = test_str.replace('\n' + value, '');
} else {
test_str = test_str.replace(value, '');
}
}
}
});
// console.log('OLD TEXT:\n' + sample_text);
// console.log('-----------------------------------------------------');
console.log('NEW TEXT:\n' + test_str);
var t1 = performance.now();
console.log("Functions Call Benchmark" + (Math.round(t1 - t0)) + " milliseconds.");
@denzndhauz
Copy link
Author

Use Case:
This file will be useful for removing a phone number or email in a certain line/sentence on a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment