Viết chương trình nhập chuỗi, thay thế các cụm 2 hoặc nhiều khoảng trắng liên tiếp thành một khoảng trắng duy nhất dùng regex /\s{2,}/g.
Ví dụ
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>REMOVE SPACES</title> </head> <body> <script> function removeSpaces() { var textInput = insertText.value; var textInput = textInput.replace(/\s{2,}/g, " "); insertText.value = textInput; } </script> <input type="text" id="insertText" value="containin extra space"> <button onclick="removeSpaces()">ok</button> </body> </html>
Kết quả trước và sau khi nhấn OK:

