-4

I was tasked with a project of creating a CV site were the user uploads a .docx file and the details is extracted from the file and is automatically inputted in the template designed by me,

I have been able to extract the details .docx file with JavaScript and the extracted details was kept in an array to make it easy to identify words with indexing. For example

[Adeola Emmanuel, adeolaemmanuel@gmail.com, pharmacist, 2 ketu ikorodu lagos, etc].

where i need help is not all CV uploaded by the user has the name coming first or email coming second but its sure that they come within 0,6 of the array so i decided to write a function that will help with that but its not working

var email = email(text.slice(0, 5));

function email(email) {
  var re = /.{1,}@[^.]{1,}/ig;
  if (!re.test(email)) {
    email = text.slice(0, 1);
    return email;
  } else if (re.test(email)) {
    email = text.slice(3, 5);
    return email;
  }
}
  • 2
    I stopped being interested, when i noticed, that you name almost every variable, function, or parameter in your code `email`. – ASDFGerte Nov 10 '19 at 13:51
  • *"the extracted details was kept in an array"*: so `text` is an array in your code? Or is it a string? You seem to use it in both senses. Could you please add some concrete input examples? What is `name`? Why do you make the regex test in the `else if` condition? Do you have any reason the result will be different the second time you run the same `.test(email)`? Is the function supposed to return a string? Why return `slice(3,5)`? Is that 2 characters or is that an array with two strings? Why? – trincot Nov 10 '19 at 13:55
  • 1
    Try renaming your variables, also there's a regex for `email` online, look that up, also you can use `String.prototype.match` function, it'll be helpful. – corvus Nov 10 '19 at 13:56
  • Yes text is an array in my code, name was meant to be email that was a mistake on my part, just like i explained the array of extracted details might not come with name starting first [Adeola Emmanuel, adeolaemmanuel@gmail.com, pharmacist, 2 ketu ikorodu lagos, etc] if that happens else if make email equals the position of the name or email – Adeola Emmanuel Nov 10 '19 at 14:13

1 Answers1

2

You can use the find array method:

function getEmail(arr) {
    let re = /\S@[^.\s]/;
    return arr.find(str => re.test(str));
}

let text = ["Adeola Emmanuel", "adeolaemmanuel@gmail.com", "pharmacist", "2 ketu ikorodu lagos"];

let email = getEmail(text.slice(0, 5));

console.log(email);

Some remarks:

  • {1,} in regular expressions can be shortened to just +
  • You actually don't need to test for multiple occurrences with +, since you would already accept one occurrence. So that also means you would be OK with just one non-point character after the @.
  • Neither of the regex suffixes (ig) have any use in your regex.
  • The .test method should get a string as argument, not an array. So you need to pass it email[0] for example.
  • For a full test of whether some string is a valid email address, the regular expression would be way more complex
  • When an if condition is false, there is no need to test the exact opposite in the else block: by exclusion that opposite condition will always be true when it gets executed.
  • The slice of an array is still an array, so returning text.slice(3, 5); in the else block does not make sense. You want to return a string.
  • You need a loop to inspect other array elements for as long as you don't have a match and have not reached the end of the array. So some loop construct is needed. You can use for, while, or any of the array methods that do such looping. find is particular useful in this case.
  • Don't give your function the same name as another variable (email) as only one value can be assigned to that variable (a function, a string, or still something else). So in your case you'll lose the function definition by the var initialisation.
trincot
  • 317,000
  • 35
  • 244
  • 286