Skip to content

Instantly share code, notes, and snippets.

@daifu
Forked from eliperelman/LICENSE.txt
Created July 24, 2012 02:06
Show Gist options
  • Save daifu/3167514 to your computer and use it in GitHub Desktop.
Save daifu/3167514 to your computer and use it in GitHub Desktop.
String.prototype.trim polyfill for 140byt.es

String.prototype.trim polyfill for 140byt.es

Adds the trim method to strings in versions of JavaScript that do not natively support it.

''.trim || (String.prototype.trim = // Use the native method if available, otherwise define a polyfill:
function () { // trim returns a new string (which replace supports)
return this.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g,'') // trim the left and right sides of the string
})
''.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g,'')})
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Eli Perelman http://eliperelman.com
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "StringPrototypeTrimPolyfill",
"description": "Adds the trim method to strings in versions of JavaScript that do not natively support it.",
"keywords": [
"string",
"prototype",
"trim",
"polyfill",
"ecmascript5"
]
}
<!DOCTYPE html>
<title>String.prototype.trim polyfill</title>
<script>
''.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g,'')})
// logs 'my awesome string'
console.log(' my awesome string '.trim());
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment