Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trim function 1

Status
Not open for further replies.

steve100

Technical User
Aug 15, 2002
35
0
0
GB
Can anyone show me a really easy way to trim strings in JS? I don't know why it is so difficult.

Thanks
Steven
 
Here is one that uses regular expressions:
Code:
function trim(_data) {
  return _data.replace(/^\s+|\s+$/g, '');
}
And you would call it like this:
Code:
var trimmedStr = trim('    hello    ');
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Isn't amazing that there's not built-in trim for Javascript? Or is just me?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top