I need a function that replaces several hyphenation with one, spaces and hyphenation from the end and beginning of the line removes (as trim for php ), replaces the hyphenation with some specific string. And all this I need to implement in javascript (jQuery is still possible and maybe other js libraries).

I would be very grateful if you help. You are welcome

  • You can three different functions. Just drop the idea - Vitaly Zaslavsky

2 answers 2

jQuery has a $.trim . I think it's easy to guess exactly what it does. but for the rest, this article can help

with the transfer, I hope this article will help

  • And the transfer, what is this character? - Vitaly Zaslavsky
  • one
    In normal browsers - "\ n", in old IE - "\ r \ n" - Zowie

What is jquery extension? What for???

 String.prototype.trim = function () { return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1"); }; 

And by the way - in modern browsers there is native support for this method, so it’s better to write this:

  if (!String.prototype.trim) { String.prototype.trim = function () { return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1"); }; } 

  var str = " HELLO "; console.log(str.trim()); // "HELLO"; 
  • Well, since I connected jQuery, then why should I waste extra memory? - Vitaly Zaslavsky
  • Extra memory? xD ok cap ... - Zowie