{"id":101,"date":"2008-11-20T16:07:54","date_gmt":"2008-11-20T21:07:54","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2008\/11\/20\/custom-trim-functions-in-javascript\/"},"modified":"2008-11-20T16:07:54","modified_gmt":"2008-11-20T21:07:54","slug":"custom-trim-functions-in-javascript","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2008\/11\/20\/custom-trim-functions-in-javascript\/","title":{"rendered":"Custom trim() functions in Javascript"},"content":{"rendered":"<p>I come from a database background, so I have always taken Oracle&#8217;s trim() functions for granted.  In short, the trim() function removes spaces at the start and end of a string, the ltrim() function removes spaces at the start (ie. left side) of a string, and finally the rtrim() function removes spaces at the end (ie. right side) of a string.  As I dabble into web development nowadays, I thought it would be handy to have similar functions in Javascript at my disposal.<\/p>\n<pre class=\"code\">\nfunction trim(txt) {\n\treturn txt.replace(\/^s+|s+$\/g,\"\");\n}\n\n\nfunction ltrim(txt) {\n\treturn txt.replace(\/^s+\/,\"\");\n}\n\n\nfunction rtrim(txt) {\n\treturn txt.replace(\/s+$\/,\"\");\n}\n<\/pre>\n<p>Below are some usage examples.<\/p>\n<pre class=\"code\">\nalert(trim(' hello '));\n\/\/ This should return 'hello'; spaces on both sides is removed\n\nalert(ltrim(' hello '));\n\/\/ This should return 'hello '; only space on left side is removed\n\nalert(rtrim(' hello '));\n\/\/ This should return ' hello'; only space on right side is removed\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This set of trim(), rtrim(), and ltrim() functions can be used to remove extraneous spaces at the start, at the end, or at both sides of a string.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}