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!

question regarding perl/javascript function

Status
Not open for further replies.

TurboSRT4

Programmer
Nov 23, 2009
47
US
hi guys, is there a javascript function equivalent of the chomp function in perl. As to remove any new line hidden characters from a string. Thanks
 
Hi

Nope. Even the [tt]String.trim()[/tt] method was introduced only in JavaScript 1.8.1 ( ~beginning of 2009 (?) ).

However, you can take advantage of the fact that JavaScript is prototype-based. So just declare your chomp() method :
JavaScript:
String[teal].[/teal][b]prototype[/b][teal].[/teal]chomp[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [b]return[/b] [b]this[/b][teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/\r?\n?$/[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])[/teal]
[teal]}[/teal]
Then use it, for example :
JavaScript:
tailoredStr[teal]=[/teal]document[teal].[/teal]form[teal][[/teal][purple]0[/purple][teal]].[/teal]comment[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]chomp[/color][teal]()[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top