最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change   to space - Stack Overflow

programmeradmin7浏览0评论

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  

Share Improve this question edited May 23, 2017 at 12:22 CommunityBot 11 silver badge asked Sep 10, 2015 at 12:13 MakudexMakudex 1,0826 gold badges15 silver badges42 bronze badges 6
  • 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 &nbsp 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
 |  Show 1 more ment

1 Answer 1

Reset to default 16

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

str.replace(/ /g, ' ');
发布评论

评论列表(0)

  1. 暂无评论