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
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
-
Well,
is a space character. What exactly are you planing to do? Where does your input e from, where do you want it to go and how is it processed (server site or client site)? – user1438038 Commented Sep 10, 2015 at 12:16 -
@user1438038 Yes, i just wanted to replace the   with space. Like this (you
and
me) would just be (you and me). – Makudex Commented Sep 10, 2015 at 12:21 -
What is wrong with
- why is this a problem? – Reinstate Monica Cellio Commented Sep 10, 2015 at 12:23 -
3
Okay, then replace it at server-side before you save it, rather than breaking it in the browser. If you have five spaces (for example) then they will be trimmed to one. If you have
5 times then it is not trimmed. Leave it as it is in the browser, as it's like that for a reason. – Reinstate Monica Cellio Commented Sep 10, 2015 at 12:27 - 1 You've got a point with that @Archer, thanks! I'll just do it with the backend. – Makudex Commented Sep 10, 2015 at 12:28
1 Answer
Reset to default 16Use the JavaScript replace
function using Regex with the global flag:
str.replace(/ /g, ' ');