Hello
I am doing a research here to find you the best way to format user text messages.
A sample of what I am trying to achieve:
1) user sends this message:
Doctor,
I would like to have
an appointment tomorrow morning.Please,call me!
2) my application formats this text outputting this:
Doctor, I would like to have an appointment tomorrow morning. Please, call me!
Notice that:
- trailing and leading spaces must gone (something like using $.trim())
- extra spaces between two words must be replaced by one space
- new lines, break lines, tabs,
must be replaced by one space - dots and commas must be separated from next word (morning.Please,call-> morning. Please, call)
Here something I have got so far:
text.replace(/<(.|\n\r)*?>/g, '')
.replace(/\s/g,' ')
.replace(/<br>/g,' ')
.replace(/ +/g,' ');
It would be good to merge all expressions in just one pattern. Is there a shorter way to do it?