What is the best way to trim() in javascript

前端 未结 19 1824
眼角桃花
眼角桃花 2020-11-29 06:32

The question says it all; JS doesn\'t seem to have a native trim() method.

相关标签:
19条回答
  • 2020-11-29 06:53

    The shortest form for jQuery:

    string = $.trim(string);
    

    Link

    0 讨论(0)
  • 2020-11-29 06:55

    For ltrim, replace spaces anchored at the start of the string with nothing:

    str2 = str.replace(/^\s+/,'');
    

    For rtrim, replace spaces anchored at the end of the string with nothing:

    str2 = str.replace(/\s+$/,'');
    

    For trim:

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

    These all use regex'es to do the actual work.

    0 讨论(0)
  • 2020-11-29 06:56

    As a couple of others have already noted, it's usually best to do this sort of thing by using a third-party JS library. Not that trim() is a complicated function to build yourself, but there are so many functions that aren't native to JavaScript that you might need and end-up writing yourself, it soon becomes more cost-effective to use a library.

    Of course, another advantage of using a JS library is that the authors do the hard work of ensuring that the functions work across all the major browsers, so that you can code to a standard interface and forget about the irritating differences between Internet Explorer and all the other browsers.

    0 讨论(0)
  • 2020-11-29 07:00

    according to this page the best all-around approach is

    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    

    Of course if you are using jQuery , it will provide you with an optimized trim method.

    0 讨论(0)
  • 2020-11-29 07:00

    This is probably not the fastest, and might violate what ".trim()" probably should really be, but I don't like RegExs (mainly because it takes so much time to figure out what they really mean/do) and I like having something that I know will work regardless of whether I have jQuery or not (not to mention the right version, since I tried $.trim(myVar) with jQuery 1.4.2 and it does not work), and will get rid of ALL extra spaces, not just at the end, rebuilding it like it should be:

    function Trim(obj) {
        var coll = "";
        var arrObj = obj.split(' ');
    
        for (var i=0;i<arrObj.length;i++) {
            if (arrObj[i] == "") {
                arrObj.splice(i,1);  // removes array indices containing spaces
            }
        }
        //alert(arrObj.length);  // should be equal to the number of words
        // Rebuilds with spaces in-between words, but without spaces at the end
        for (var i=0;i<arrObj.length;i++) {
            if (arrObj[i] != "" && i != arrObj.length-1)
                coll += arrObj[i] + " ";
            if (arrObj[i] != "" && i == arrObj.length-1)
                coll += arrObj[i];
        }
    
        return coll;
    }
    
    0 讨论(0)
  • 2020-11-29 07:00

    You can use following ...

    function trim(str) {
        try {
            if (str && typeof(str) == 'string') {
                return str.replace(/^\s*|\s*$/g, "");
            } else {
                return '';
            }
        } catch (e) {
            return str;
        }
    }
    
    0 讨论(0)
提交回复
热议问题