user3142695
user3142695

Reputation: 17332

How to remove only dots after initial character without followed space character

I need to remove dots after initials (without following space) and the comma before them.

If this is the input

Some, A.B., Author, D., Names, M.F. Lorem ipsum. Lorem ipsum.

...the result should be

Some AB, Author D, Names MF. Lorem ipsum. Lorem ipsum.

I tried to get the correct regex to use it with replace:

string.replace(/(, [A-Z])\./g, '$1')

But this is not working, as expected and I do not see what is going wrong.

Upvotes: 0

Views: 42

Answers (1)

revo
revo

Reputation: 48711

You are almost there. You should make comma and preceding spaces optional and add a negative lookahead:

var s = 'Some, A.B., Author, D., Names, M.F. Lorem ipsum. Lorem ipsum.';
console.log(s.replace(/,?( *)([A-Z])\.(?!\s)/g, '$1$2'));

Upvotes: 1

Related Questions