Makudex
Makudex

Reputation: 1082

How to change   to space

I am developing a simple rich text editor by using contenteditable div. Now, I what I want is to change the   to a space.

I found this similar question How to replace   to space? in which the solution is to use REGEX but the answer doesn't provide any sample code and I really don't know how to use that.

I have this javascript code here:

if (e.keyCode == 32) {
    document.execCommand('inserText', false, ' ');
    return false;
}

In which as the user presses space, a space would insert as a text but by retrieving it, it's value is  

Upvotes: 6

Views: 16104

Answers (1)

Nerdwood
Nerdwood

Reputation: 4022

Use the JavaScript replace function using Regex with the global flag:

str.replace(/ /g, ' ');

Upvotes: 16

Related Questions