working with JavaScript replace method

Hi experts,

I have this fiddle example:
https://jsfiddle.net/n5ydqwyx/1/

I have a text box on my page.
When I type in the textbox, anytime I type a comma, the comma gets replaced with a comma plus a space
Only time that a space is not put after a comma is when it's followed by an equal sign.

For example when i type in the textbox ,everytime I typed a comma it put a space right after it like this:
CommaSDeletesCharacter.PNG
There is one issue though. The character that I typed right after the comma gets deleted.

For example if I type  ,m then that gets replaced with comma and a space so it looks like this:

m.PNG
Any character that I typed right after the comma should not get deleted.

For example if I type ,p then it should display a comma then a space then a p after the space like this:

, p

For example, if I typed this in the textbox:

MTKE,KL,TEHRT,W

I should get this:

MTKE, KL, TEHRT, W

But I'm getting this:

n.PNG
It's deleting the first character that I typed after each comma.



Anyone know How I can fix my fiddle?
LVL 1
maqskywalkerAsked:
Who is Participating?
 
Julian HansenCommented:
Try this
 this.value = this.value.replace(/,([^\= ])/g, ', $1');

Open in new window

0
 
maqskywalkerAuthor Commented:
Really nice! Thanks!
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.