471,350 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Does Trim works in JavaScript ?

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
3 1643
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
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
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.

Similar topics

11 posts views Thread by Reply Via Newsgroup | last post: by
3 posts views Thread by Shailesh Humbad | last post: by
7 posts views Thread by Henri | last post: by
19 posts views Thread by Prisoner at War | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.