0

I have been trying to employ a regular expression to grab sentences from the following paragraph:

str="Hello, my name is Mr. Bob.  How are you? I am in the F.B.I. My favorite number is 2.5."   

var res = str.match( /[^\.!\?]+[\.!\?]+/g ); 

console.log(res);

Here is the result:

["Hello, my name is Lance.", "  How are you?", " I am in the F.", "B.", "I.", " My favorite number is 2.", "5."]

How can I capture "F.B.I." and "2.5" as words in a sentence and not a bunch of individual sentences?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Lance
  • 123
  • 2
  • 14
  • can you please "define" sentence in a clear English sentence – Gavriel Feb 06 '16 at 19:30
  • Normal string functions? – Jan Feb 06 '16 at 19:30
  • Take a look at [this](http://stackoverflow.com/a/35109078/3764814) - it's not easy in the general case. – Lucas Trzesniewski Feb 06 '16 at 19:32
  • 1
    I didn't get the Mr. That's hard. The rest: `var res = str.match( /[A-Z].*?[\.\!\?]( |$)/g);` – Sigfried Feb 06 '16 at 19:41
  • 1
    Generally, it's not a trivial task. Check out [this](http://stackoverflow.com/questions/34881790/split-string-into-sentences-using-regex) answer. – ndnenkov Feb 06 '16 at 20:45
  • The example you all have shared is insightful in terms of informing me of the scale of work one must go through to solve this problem. However, your linked solution is in php and I have no familiarity with php unfortunately. Is php to javascript conversion difficult? Especially when dealing with such a complex issue? Thanks! – Lance Feb 07 '16 at 13:32

1 Answers1

0

A space symbol or the end of the string might be a good hint that it's the end of the sentence.

str="Hello, my name is Mr. Bob.  How are you? I am in the F.B.I. My favorite number is 2.5."   
console.log(str.match( /.*?[\.!\?]+(?:\s|$)/g ));

Which outputs:

["Hello, my name is Mr. ", "Bob. ", " How are you? ", "I am in the F.B.I. ", "My favorite number is 2.5."]

Bob can't be captured except explicitly specifying some words that are usually followed by a dot (like Mr, Ms, Mrs, etc) and, as mentioned by @ndn in comments, a more detailed research can be found here

Community
  • 1
  • 1
Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56