zagbala
zagbala

Reputation: 7

Remove and replace consecutive characters

I have that content:

ALL                                  EVERYTHING

I want to remove all   and get that string: ALL EVERYTHING.

If I use that code:

  var str = $("#post_wall_textarea_parent .emojionearea-editor").html(); // I can't use $("#post_wall_textarea_parent .emojionearea-editor").text(); for many reasons
  str_  = str.replace(/ /g, " ");

It gives me that: ALL EVERYTHING.

I want to remove more thant one space between two words.

How could I do to get: ALL EVERYTHING instead of ALL EVERYTHING. ?

Thanks.

Upvotes: 1

Views: 109

Answers (1)

Mohit Yadav
Mohit Yadav

Reputation: 465

Use str.replace(/( )+/g, " ");

const str = "ALL                                  EVERYTHING";

const result = str.replace(/( )+/g, " ");

console.log(result);

`Edited as Dimitri's Comment.

Upvotes: 1

Related Questions