-3
\$\begingroup\$

Task: Parse sentences, even multiline

Tests:

Hello. I'm dog. to ['Hello.',"I'm dog."]

Hello.I'm dog. to ['Hello.',"I'm dog."]

Hello.\n I'm dog. to ['Hello.',"I'm dog."]

Hello.\n\n\n\n I'm dog. to ['Hello.',"I'm dog."]

I want to know how to improve my code or make shorter and clener.

function parseSentences(data) {
    return data
      .replace('.', '. ')
      .replace(/(\.+|:|!|\?)("*|'*|\)*|}*|]*)(\s|\n|\r|\r\n)/gm, '$1$2|')
      .split('|')
      .map(element => element.trim())
  }
\$\endgroup\$
1
  • 2
    \$\begingroup\$ How exactly do you define a "sentence"? Please specify what the task is in further detail, particularly how punctuation is to be treated. \$\endgroup\$ Feb 1 '18 at 20:54
2
\$\begingroup\$

I would do different functions for each different replace and split, and then joy every call together in another function. Maybe you could give names for each rule, to make it more clear why you are doing what you are doing... Maybe "data" and "element" as var names are too general, I would specify a little bit more.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.