Regex to replace multiple spaces with a single space

后端 未结 23 2325
北恋
北恋 2020-11-22 10:00

Given a string like:

\"The dog      has a long   tail, and it     is RED!\"

What kind of jQuery or JavaScript magic can be used to keep spaces to only o

相关标签:
23条回答
  • 2020-11-22 10:07

    Comprehensive unencrypted answer for newbies et al.

    This is for all of the dummies like me who test the scripts written by some of you guys which do not work.

    The following 3 examples are the steps I took to remove special characters AND extra spaces on the following 3 websites (all of which work perfectly) {1. EtaVisa.com 2. EtaStatus.com 3. Tikun.com} so I know that these work perfectly.

    We have chained these together with over 50 at a time and NO problems.

    // This removed special characters + 0-9 and allows for just letters (upper and LOWER case)

    function NoDoublesPls1()
    {
    var str=document.getElementById("NoDoubles1");
    var regex=/[^a-z]/gi;
    str.value=str.value.replace(regex ,"");
    }
    

    // This removed special characters and allows for just letters (upper and LOWER case) and 0-9 AND spaces

    function NoDoublesPls2()
    {
    var str=document.getElementById("NoDoubles2");
    var regex=/[^a-z 0-9]/gi;
    str.value=str.value.replace(regex ,"");
    }
    

    // This removed special characters and allows for just letters (upper and LOWER case) and 0-9 AND spaces // The .replace(/\s\s+/g, " ") at the end removes excessive spaces // when I used single quotes, it did not work.

    function NoDoublesPls3()
    {    var str=document.getElementById("NoDoubles3");
    var regex=/[^a-z 0-9]/gi;
    str.value=str.value.replace(regex ,"") .replace(/\s\s+/g, " ");
    }
    

    ::NEXT:: Save #3 as a .js // I called mine NoDoubles.js

    ::NEXT:: Include your JS into your page

     <script language="JavaScript" src="js/NoDoubles.js"></script>
    

    Include this in your form field:: such as

    <INPUT type="text" name="Name"
         onKeyUp="NoDoublesPls3()" onKeyDown="NoDoublesPls3()" id="NoDoubles3"/>
    

    So that it looks like this

    <INPUT type="text" name="Name" onKeyUp="NoDoublesPls3()" onKeyDown="NoDoublesPls3()" id="NoDoubles3"/>
    

    This will remove special characters, allow for single spaces and remove extra spaces.

    0 讨论(0)
  • 2020-11-22 10:09

    This is one solution, though it will target all space characters:

    "The      dog        has a long tail,      and it is RED!".replace(/\s\s+/g, ' ')
    
    "The dog has a long tail, and it is RED!"
    

    Edit: This is probably better since it targets a space followed by 1 or more spaces:

    "The      dog        has a long tail,      and it is RED!".replace(/  +/g, ' ')
    
    "The dog has a long tail, and it is RED!"
    

    Alternative method:

    "The      dog        has a long tail,      and it is RED!".replace(/ {2,}/g, ' ')
    "The dog has a long tail, and it is RED!"
    

    I didn't use /\s+/ by itself since that replaces spaces that span 1 character multiple times and might be less efficient since it targets more than necessary.

    I didn't deeply test any of these so lmk if there are bugs.

    Also, if you're going to do string replacement remember to re-assign the variable/property to its own replacement, eg:

    var string = 'foo'
    string = string.replace('foo', '')
    

    Using jQuery.prototype.text:

    var el = $('span:eq(0)');
    el.text( el.text().replace(/\d+/, '') )
    
    0 讨论(0)
  • 2020-11-22 10:09

    ' mouse pointer touch '.replace(/^\s+|\s+$|(\s)+/g, "$1") should do the trick!

    0 讨论(0)
  • 2020-11-22 10:12

    Given that you also want to cover tabs, newlines, etc, just replace \s\s+ with ' ':

    string = string.replace(/\s\s+/g, ' ');
    

    If you really want to cover only spaces (and thus not tabs, newlines, etc), do so:

    string = string.replace(/  +/g, ' ');
    
    0 讨论(0)
  • 2020-11-22 10:12

    I have this method, I call it the Derp method for lack of a better name.

    while (str.indexOf("  ") !== -1) {
        str = str.replace(/  /g, " ");
    }
    

    Running it in JSPerf gives some surprising results.

    0 讨论(0)
  • 2020-11-22 10:14

    I know that I am late to the party, but I discovered a nice solution.

    Here it is:

    var myStr = myStr.replace(/[ ][ ]*/g, ' ');
    
    0 讨论(0)
提交回复
热议问题