相关文章推荐
谦逊的板栗  ·  c++ - In CMake, how ...·  7 月前    · 
谦逊的板栗  ·  jpa repository ...·  11 月前    · 
谦逊的板栗  ·  rowid and rownum in ...·  11 月前    · 
谦逊的板栗  ·  ORA-01654 unable to ...·  11 月前    · 
谦逊的板栗  ·  文件系统 | 微信开放文档·  11 月前    · 
谦逊的板栗  ·  Keeping Web Shells ...·  1 年前    · 
坏坏的麦片  ·  SqlParameter.Value ...·  2 小时前    · 
爱旅游的铁链  ·  Uses of Class ...·  2 小时前    · 
独立的红豆  ·  SqlParameter ...·  2 小时前    · 
聪明伶俐的课本  ·  SpringBoot ...·  3 小时前    · 

Remove spaces with replace() using a regular expression:

let text = "       Hello World!        ";
let result = text.replace(/^\s+|\s+$/gm,''); Try it Yourself »

Description

The trim() method removes whitespace from both sides of a string.

The trim() method does not change the original string.

See Also:

The trimEnd() Method

The trimStart() Method

The padEnd() Method

The padStart() Method

Syntax

string .trim()

Parameters

Return Value

Description A stringA string with removed whitespace from both ends.

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use , cookie and privacy policy .

W3Schools is Powered by W3.CSS .

 
推荐文章