regex - Select only sentences in javascript -
i have been trying employ regular expression grab sentences following paragraph:
str="hello, name mr. bob. how you? in f.b.i. favorite number 2.5." var res = str.match( /[^\.!\?]+[\.!\?]+/g ); console.log(res); here result:
["hello, name lance.", " how you?", " in f.", "b.", "i.", " favorite number 2.", "5."] how can capture "f.b.i." , "2.5" words in sentence , not bunch of individual sentences?
a space symbol or end of string might hint it's end of sentence.
str="hello, name mr. bob. how you? in f.b.i. favorite number 2.5." console.log(str.match( /.*?[\.!\?]+(?:\s|$)/g )); which outputs:
["hello, name mr. ", "bob. ", " how you? ", "i in f.b.i. ", "my favorite number 2.5."] bob can't captured except explicitly specifying words followed dot (like mr, ms, mrs, etc) and, mentioned @ndn in comments, more detailed research can found here
Comments
Post a Comment