Before we start, there's a post about replacing more than one white spaces to only one horizontal space . Modern Browser To trim leading / trailing whitespaces on modern browser, we can simply use trim() method: string.trim(); Example: let string = " this has many random "; let result = string.trim(); // "result" → "this has many random". Regular Expression /^\s+|\s+$/g Details: The ^ is to match the starting string. The $ is to match the back of the string. The \s is to match all white space criteria (horizontal space, tab, carriage, and so on). The + is to match one or more repetition. The | is the OR operator. The g is to match globally (the entire string). So this regular expression pattern /^\s+|\s+$/g can be translated to words as: Find all (1 or more repetition of) whitespace which is located in front OR at the back of the entire string. To be more complete: /^[\s\uFEFF\xA0]+|[\s...
Reflections on ECMAScript (JavaScript), CSS, HTML, Blogger, technical fixes, literature, music, cinema, entertainment in general, the English language, mathematics, history, trousers-related, and curiosities of the realm.