Hello! Faced an interesting task: there is a line that can contain non-printing characters (space, carriage return, tab, etc.). It is necessary to remove all these characters, but only from the right and left edges of the line. If spaces are caught in the middle of a line (between printable characters), then they must be left. Help me please! The problem is to remove the carriage transfer, I could not cope with it.
1 answer
First, .trim() , but if you want regexp, then new RegExp('(^\\s+|\\s+$)', 'g') or so .replace(/(^\s+|\s+$)/g,"")
Secondly, if you fantasize and need clean paragraphs in a line, then this expression will remove everything left and right in each paragraph of the line: .trim().replace(/\s*\n\s*/g,"\n") - there will be only one line break between paragraphs.
|
mystring.trim(). - Wiktor Stribiżew