Comments on: String Functions http://mattsnider.com/languages/javascript/string-functions/ Understanding JavaScript and Frameworks Thu, 28 Aug 2008 01:22:30 +0000 http://wordpress.org/?v=2.1.2 By: Dimitry http://mattsnider.com/languages/javascript/string-functions/#comment-3481 Dimitry Sat, 19 Jan 2008 01:30:52 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-3481 Very useful! Thanks for posting this Very useful! Thanks for posting this

]]>
By: Kravvitz http://mattsnider.com/languages/javascript/string-functions/#comment-3652 Kravvitz Mon, 21 Jan 2008 23:21:13 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-3652 1) Something seems to have "eaten" the backslashes in those regular expressions. 2) Not all of the characters in the regular expressions were converted into X/HTML character entities correctly. 3) You have some "curly" quotes in there which will probably cause problems. 4) The statement in the trim function can be simplified to this: return this.replace(/^\s*|\s*$/g,''); (In case the backslashes are messed up here too, there should be one backslash preceding each of the two "s" characters.) 1) Something seems to have “eaten” the backslashes in those regular expressions.

2) Not all of the characters in the regular expressions were converted into X/HTML character entities correctly.

3) You have some “curly” quotes in there which will probably cause problems.

4) The statement in the trim function can be simplified to this:
return this.replace(/^\s*|\s*$/g,”);
(In case the backslashes are messed up here too, there should be one backslash preceding each of the two “s” characters.)

]]>
By: admin http://mattsnider.com/languages/javascript/string-functions/#comment-3653 admin Mon, 21 Jan 2008 23:28:37 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-3653 Thanks Krawitz. I missed that WordPress drops the backslashes. This messed up most of the expressions in this article. I went ahead and replaced them with HTMLEntities and I believe all the expressions are correct now. Thanks Krawitz.

I missed that WordPress drops the backslashes. This messed up most of the expressions in this article. I went ahead and replaced them with HTMLEntities and I believe all the expressions are correct now.

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17540 Badotz Mon, 30 Jun 2008 21:50:38 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17540 I also include >ltrimrtrimchop len) { var tmp = String(str).substr(0, len); var x = tmp.lastIndexOf(' '); if (x > 0) { out.push(this.trim(tmp.substr(0, x))); str = this.trim(str.substr(x + 1)); } else { out.push(tmp.substr(0, len)); str = str.substr(len + 1); } } else { out.push(str); str = ''; } } return ((glue === undefined) ? out : out.join(glue)); }, I also include >ltrimrtrimchop len)
{
var tmp = String(str).substr(0, len);
var x = tmp.lastIndexOf(’ ‘);
if (x > 0)
{
out.push(this.trim(tmp.substr(0, x)));
str = this.trim(str.substr(x + 1));
}
else
{
out.push(tmp.substr(0, len));
str = str.substr(len + 1);
}
}
else
{
out.push(str);
str = ”;
}
}
return ((glue === undefined) ? out : out.join(glue));
},

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17541 Badotz Mon, 30 Jun 2008 21:51:58 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17541 Wow, did *that* ever get mangled :-( /** * Trim leading spaces */ ltrim: function(s) { return s.replace(/\\s*((\\S+\\s*)*)/, '$1'); }, Wow, did *that* ever get mangled :-(

/**
* Trim leading spaces
*/
ltrim: function(s) { return s.replace(/\\s*((\\S+\\s*)*)/, ‘$1′); },

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17542 Badotz Mon, 30 Jun 2008 21:52:34 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17542 Too many backslashes that time :-/ What a P.O.S. Too many backslashes that time :-/ What a P.O.S.

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17544 Badotz Mon, 30 Jun 2008 21:53:10 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17544 Last chance: /** * Trim leading spaces */ ltrim: function(s) { return s.replace(/\s*((\S+\s*)*)/, '$1'); }, Last chance:

/**
* Trim leading spaces
*/
ltrim: function(s) { return s.replace(/\s*((\S+\s*)*)/, ‘$1′); },

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17545 Badotz Mon, 30 Jun 2008 21:53:33 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17545 /** * Trim trailing spaces */ rtrim: function(s) { return s.replace(/((\s*\S+)*)\s*/, '$1'); }, /**
* Trim trailing spaces
*/
rtrim: function(s) { return s.replace(/((\s*\S+)*)\s*/, ‘$1′); },

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17546 Badotz Mon, 30 Jun 2008 21:53:55 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17546 /** * Trim leading and trailing spaces */ trim: function(s) { return s.ltrim(s.rtrim(s)); }, /**
* Trim leading and trailing spaces
*/
trim: function(s) { return s.ltrim(s.rtrim(s)); },

]]>
By: Badotz http://mattsnider.com/languages/javascript/string-functions/#comment-17547 Badotz Mon, 30 Jun 2008 21:54:42 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17547 And finally: /** chop a string into segments of 'n' chars */ chop: function(str, len, glue) { var out = []; len = ((len === undefined || isNaN(len)) ? str.length : parseInt(len)); while (str != '') { if (str.length > len) { var tmp = String(str).substr(0, len); var x = tmp.lastIndexOf(' '); if (x > 0) { out.push(this.trim(tmp.substr(0, x))); str = this.trim(str.substr(x + 1)); } else { out.push(tmp.substr(0, len)); str = str.substr(len + 1); } } else { out.push(str); str = ''; } } return ((glue === undefined) ? out : out.join(glue)); }, And finally:

/**
chop a string into segments of ‘n’ chars
*/
chop: function(str, len, glue) {
var out = [];
len = ((len === undefined || isNaN(len)) ? str.length : parseInt(len));
while (str != ”)
{
if (str.length > len)
{
var tmp = String(str).substr(0, len);
var x = tmp.lastIndexOf(’ ‘);
if (x > 0)
{
out.push(this.trim(tmp.substr(0, x)));
str = this.trim(str.substr(x + 1));
}
else
{
out.push(tmp.substr(0, len));
str = str.substr(len + 1);
}
}
else
{
out.push(str);
str = ”;
}
}
return ((glue === undefined) ? out : out.join(glue));
},

]]>
By: Matt Snider http://mattsnider.com/languages/javascript/string-functions/#comment-17554 Matt Snider Mon, 30 Jun 2008 23:03:23 +0000 http://mattsnider.com/languages/javascript/string-functions/#comment-17554 Thanks for the additional methods Badotz. Thanks for the additional methods Badotz.

]]>