Remove   if there are more than 10

I want to allow only a maximum of 10   between any two words and remove the remaining  . How can I do this in JavaScript with a regex?

  • Ckeditor trigger widget's dialog on upcast
  • Generate Non-HTML Markup from CKEditor
  • CKEditor link dialog removing protocol
  • CKEditor: Apply removeFormat on paste
  • How can I highlight elements that match a regex in CKeditor?
  • Retain cursor position after reloading the page in CKEditor
  • CKEditor, initialize widget added with insertElement
  • remove <p> &nbsp;</p> with jquery if no value
  • Wrap CKEditor WYSYWG content with additional (non editable) HTML to apply element specific formatting
  • Ckeditor : How can i make few tags like h3 , h4 , h5 non editable in ckeditor
  • How to find and replace text in CKEditor using Javascript?
  • Paste from word with uppercase
  • 4 Solutions collect form web for “Remove &nbsp; if there are more than 10”

    str.replace(/\ {11,}/g, "          ");
    

    Alternatively:

    str.replace(/(\ {10})\ */g, "$1")
    

    You don’t have to use Regex for this requirement. We are going to use the split method of the JavaScript String object in a simple function as follows:

    function firstTen(txt){
    arr = txt.split(" ");
    out = '';
    for (i = 0; i < arr.length; i++){
    if (i < 10){
    out += arr[i]+" ";
    }
    else{
    out += arr[i];
    }
    }
        return out;
    }
    txt = "1 2 3 4 5 6 7 8 9 10 Apple Egypt Africa"
        alert(firstTen(txt));‚Äč
    

    The following is a demo: http://jsfiddle.net/saidbakr/KMQAV/

    I would first create a variable with 10 &nbsp;‘s

    for (var spaces = '', i = 0; i < 10; i++) spaces += '&nbsp;';
    

    then I would use it as replacement in the following regex(p)

    str = str.replace(/([^\s])?(\s|&nbsp;){11,}(?=[^\s]|$)/g, '$1'+spaces)
    

    Here’s the breakdown of the the pattern:

    ([^\s])?          # 0 or 1 character other than white space
    (\s|&nbsp;){11,}  # any white space or &nbsp; used more than 10
    (?=[^\s]|$)       # followed by a character other than a white space
                      # or it is the end of string
    

    EDIT: I replaced the word boundary characters (\b) in the pattern because it did not match unicode character boundaries.