简体   繁体   中英

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.

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> 

Is there anything it shouldn't match?

Otherwise just use /./g

It accepts everything accept a new line

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM