Reputation: 15006
The following characters: .?!,… (maybe more, open for suggestions), should always be outputted directly after a word, and always with a space after it.
correct:
This is a fox. It is Brown.
Wrong:
This is a fox . It is Brown.
This is a fox.It is Brown
This is a fox .It is Brown.
I basically want any punctuation. except this:
[^\s][\.?!,…][\s]
Upvotes: 0
Views: 30
Reputation: 163362
You might use lookarounds if supported to assert what is on the left is not a whitespace char and what is on the right is not a non whitespace char
(?<!\s)[.?!,…](?!\S)
Edit
If you want to match the opposite and replace with a dot and space, you might use an alternation with 2 capturing groups and a callback function.
\s+([.?!,…])\s*|([^\s.?!,…][.?!,…])(?!\s)
const regex = /\s+([.?!,…])\s*|([^\s.?!,…][.?!,…])(?!\s)/g;
let str = `This is a fox. It is Brown.
This is a fox . It is Brown.
This is a fox.It is Brown
This is a fox .It is Brown.
`;
str = str.replace(regex, function(_, g1, g2) {
return (g1 || g2) + " ";
});
console.log(str);
Upvotes: 2