0

I am trying to add a space after every period (full stop). But not if it is a decimal point or an abbreviation.Using javascript I have used the following regex:

text.replace(/([^.]*?[a-zA-Z][^.0-9]*?)\.([\S][^.]*?)|([^.]*?[0-9])\.([^\s0-9][^.]*?)/g, "$1. $2")

Sample string:

An apple a day.keeps the doctor away.apple 3.0 is simply amazing.Iphone 5.is not bad either.

Output:

An apple a day. keeps the doctor away. apple 3.0 is simply amazing. I. s not bad either.

The phone disappears for some weird reason. I used regexpal.com and it is able to find the portions as expected. There is an issue with the replacement.

Also, I would appreciate suggestions for some great find and replace debuggers.

5
  • What's the abbreviation example? Do you mean an acronym? Commented Jan 8, 2014 at 20:59
  • 1
    How do you intend to tell the difference between the end of a sentence and dot-syntax code (foo.bar) or email addresses ([email protected]), etc.? Regex won't help you with that. Commented Jan 8, 2014 at 20:59
  • adding an url to the test could help Commented Jan 8, 2014 at 21:00
  • A nice debugger for Regex is RegExr. Commented Jan 8, 2014 at 21:04
  • yes, by abbreviation I imply acronym Commented Jan 8, 2014 at 21:06

1 Answer 1

2

You haven't escaped the periods, so some characters are being stripped out. You are also leaving off the $3 and $4. Since they fully alternate each other, the following should work (and will work with your example):

replace(/([^.]?[a-zA-Z][^.0-9]?)\.([\S][^.]?)|([^.]?[0-9])\.([^\s0-9][^.]*?)/g,
    "$1$3. $2$4")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.