Trim spaces from start and end of string

前端 未结 14 1315
情歌与酒
情歌与酒 2020-11-28 01:56

I am trying to find a way to trim spaces from the start and end of the title string. I was using this, but it doesn\'t seem to be working:

title = title.repl         


        
相关标签:
14条回答
  • 2020-11-28 02:34

    This is what is suggested by JavaScript Architect/Guru Douglas Crockford.

    String.method('trim', function (  ) {
        return this.replace(/^\s+|\s+$/g, '');
    });
    

    Note: you have to define "method" for Function.prototype.

    Alternatively

    String.prototype.trim = function () {
       return this.replace(/^\s+|\s+$/g, '');
    };
    
    title.trim();    // returns trimmed title
    

    Observation

    In recent browsers, the trim method is included by default. So you don't have to add it explicitly.

    Major browsers Chrome, Firefox, Safari etc. supports trim method. Checked in Chrome 55.0.2883.95 (64-bit), Firefox 51.0.1 (64-bit), Safari 10.0 (12602.1.50.0.10).

    0 讨论(0)
  • 2020-11-28 02:35

    I know this is an old post, but just thought I'd share our solution. In the quest for shortest code (doesn't everyone just love terse regex), one could instead use:

    title = title.replace(/(^\s+|\s+$)/g, '');
    

    BTW: I ran this same test through the link shared above blog.stevenlevithan.com -- Faster JavaScript Trim and this pattern beat all the other HANDS down!

    Using IE8, added test as test13. The results were:

    Original length: 226002
    trim1: 110ms (length: 225994)
    trim2: 79ms (length: 225994)
    trim3: 172ms (length: 225994)
    trim4: 203ms (length:225994)
    trim5: 172ms (length: 225994)
    trim6: 312ms (length: 225994)
    trim7: 203ms (length: 225994)
    trim8: 47ms (length: 225994)
    trim9: 453ms (length: 225994)
    trim10: 15ms (length: 225994)
    trim11: 16ms (length: 225994)
    trim12: 31ms (length: 225994)
    trim13: 0ms (length: 226002)

    0 讨论(0)
  • 2020-11-28 02:36

    Note: As of 2015, all major browsers (including IE>=9) support String.prototype.trim(). This means that for most use cases simply doing str.trim() is the best way of achieving what the question asks.


    Steven Levithan analyzed many different implementation of trim in Javascript in terms of performance.

    His recommendation is:

    function trim1 (str) {
        return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    }
    

    for "general-purpose implementation which is fast cross-browser", and

    function trim11 (str) {
        str = str.replace(/^\s+/, '');
        for (var i = str.length - 1; i >= 0; i--) {
            if (/\S/.test(str.charAt(i))) {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return str;
    }
    

    "if you want to handle long strings exceptionally fast in all browsers".

    References

    • blog.stevenlevithan.com -- Faster JavaScript Trim
    0 讨论(0)
  • 2020-11-28 02:36

    When the DOM is fully loaded, you can add this to all the text fields. I have never had a situation where I needed to submit leading or trailing space, so doing it all the time globally has worked for me...

    $(function() { $('input[type=text]').on('blur', function(){
        $(this).val($.trim($(this).val()));
      });
    });
    
    0 讨论(0)
  • 2020-11-28 02:37

    As @ChaosPandion mentioned, the String.prototype.trim method has been introduced into the ECMAScript 5th Edition Specification, some implementations already include this method, so the best way is to detect the native implementation and declare it only if it's not available:

    if (typeof String.prototype.trim != 'function') { // detect native implementation
      String.prototype.trim = function () {
        return this.replace(/^\s+/, '').replace(/\s+$/, '');
      };
    }
    

    Then you can simply:

    title = title.trim();
    
    0 讨论(0)
  • 2020-11-28 02:37

    Just use string.trim() method. It's supported by all major browsers. Reference here: http://www.w3schools.com/jsref/jsref_trim_string.asp

    0 讨论(0)
提交回复
热议问题