By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

Does Trim works in JavaScript ?

P: n/a
mhk
Hi,

i used Trim(my_val) and its giving error in JavaScript. is it a correct
way to trim. Please suggest me, thanks in advance.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
mhk wrote:
Hi,

i used Trim(my_val) and its giving error in JavaScript. is it a correct
way to trim. Please suggest me, thanks in advance.


Read the Group FAQ, it has snippets of code for emulating Trim as
Javascript has no native Trim()

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

P: n/a
mhk


Randy Webb wrote:
mhk wrote:
Hi,

i used Trim(my_val) and its giving error in JavaScript. is it a
correct way to trim. Please suggest me, thanks in advance.


Read the Group FAQ, it has snippets of code for emulating Trim as
Javascript has no native Trim()


*******************************************
the code in your FAQ doesnt handle enter keys. this one is much better.
found on internet.

// Trim Function
// --------------
String.prototype.trim = function()
{
// skip leading and trailing whitespace
// and return everything in between
var x=this;
x=x.replace(/^\s*(.*)/, "$1");
x=x.replace(/(.*?)\s*$/, "$1");
return x;
}
Jul 23 '05 #3

P: n/a
mhk wrote on 16 jan 2005 in comp.lang.javascript:
Read the Group FAQ, it has snippets of code for emulating Trim as
Javascript has no native Trim()

*******************************************
the code in your FAQ doesnt handle enter keys.


Whose FAQ?

Keys are not part of a string, so what do you mean|?
this one is much better.
found on internet.
// Trim Function
// --------------
String.prototype.trim = function()
{
// skip leading and trailing whitespace
// and return everything in between
var x=this;
x=x.replace(/^\s*(.*)/, "$1");
x=x.replace(/(.*?)\s*$/, "$1");
return x;
}


a bit overdone regex, methinks.

x = x.replace(/^\s*/, "");
x = x.replace(/\s*$/, "");

or

x = x.replace(/^\s*(.*?)\s*$/, "$1");

Problem with your and this last one is,
that older js may not know lookahead "?"
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.