Akaron
Akaron

Reputation: 33

JS regex for match sentence

I'm not able to create a regex for correctly match single sentences in a text with javascript. I've used /([!?.:])\s+/g but doesn't match a correct sentence in a long sequence.

For example

[1](I have received a big present!) [2](You know?)
[3](it's, really, a car.) [4](The car have: a blue color)

[5](It's very hardly to drive.)

Total senctences: 5 What can be a correct regex for match a sentence?

Edit:

I've numbered all the sentences. I want to match a single sentence separated by . or \n\n or \n or ! etc.

Upvotes: 0

Views: 5224

Answers (3)

befzz
befzz

Reputation: 1292

I have received a big present!
You know?
it's, really, a car.
The car have: a blue color
It's very hardly to drive.

([^ \r\n][^!?\.\r\n]+[\w!?\.]+)

var m=$("#txa")[0].defaultValue.match(/([^ \r\n][^!?\.\r\n]+[\w!?\.]+)/g);

$("<pre>"+m.join('<br/>')+"</pre>").appendTo('body')
//document.write(m.join('\n'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea cols=80 rows=7 id="txa">
  I have received a big present! You know?
it's, really, a car. The car have: a blue color 

It's very hardly to drive.
</textarea>

Upvotes: 4

Ahosan Karim Asik
Ahosan Karim Asik

Reputation: 3299

Try following this

var str='this is a pen. i am a boy';
var res= str.split(/[\.\!]+\s*|\n+\s*/);  //this code return a array of sentense.
alert(res);
console.log(res);

Upvotes: 0

user3106974
user3106974

Reputation: 110

Is there anything it shouldn't match?

Otherwise just use /./g

It accepts everything accept a new line

Upvotes: -1

Related Questions