Home > Backend Development > PHP Tutorial > Js实现类似PHP里trim方法

Js实现类似PHP里trim方法

WBOY
Release: 2016-06-20 12:32:13
Original
1064 people have browsed it

var trim = function(a,b){    var a = a ? a : " ";    var b = b ? b : "";    if (b == ""){	return "";    }    var la = a.length;    var lb = b.length;    var s = e = false;     if(b.indexOf(a) === 0){    	s = true;    }    if(b.lastIndexOf(a) === lb-la) {	e = true;	    }    var r = b;    if (e) {	if (r.length > 0) {		r = r.substring(0,lb-la);		return trim(a,r);	} else {	    return "";	}    }    if (s) {	if (r.length > 0) {	    r = r.substring(la,r.length);	    return trim(a, r);	} else {	    return "";	}    }    return r;}
Copy after login

Js里没有类似PHP里trim方法,自己实现一个,方便自用。如果在我之前有牛人已实现,思想也一致,也请不要嘲笑。确属巧合,因为我确实也没有借鉴任何人。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template