86
Views
4
Comments
Solved
How to remove extra spaces between words in a string, leaving only one space?
Question
Application Type
Reactive

How to remove extra spaces between words in a string, leaving only one space?

2025-09-21 06-51-05
Mohd Anees Mansoori
Solution

Hello ZH T,

Use below JS function and pass your string as a parameter, it will return you the desired string as an output.

function removeExtraSpaces(inputString) {

// Use regular expression to replace 1 to 200 spaces with a single space

   return inputString.replace(/\s{1,200}/g, ' ');

}

// Example usage

var inputString = $Inputparameter

$outputparameter = removeExtraSpaces(inputString);

Example:

Inputparameter = "India  USA     China         UK" 

outputparameter  = "India USA China UK" 

Also refer to the oml provided.

Hope that helps.

Regards

Anees

ForumDemo.oml
2023-12-22 05-02-09
_N_C_
2023-02-26 23-17-33
Kshitij Raheja

Hey @ZH T 

I made a brute-force solution for this 

i am looping the variable until there are no 2 spaces left and I keep replacing  2 spaces with 1

EDIT: If Condition (Index(Var1,"  ") <> -1) and Assign: Replace(Var1,"  "," ")

After that, you can run trim to make sure do not have any leading and trailing spaces

Hope this helps

Regards,
Kshitij

2025-09-21 06-51-05
Mohd Anees Mansoori
Solution

Hello ZH T,

Use below JS function and pass your string as a parameter, it will return you the desired string as an output.

function removeExtraSpaces(inputString) {

// Use regular expression to replace 1 to 200 spaces with a single space

   return inputString.replace(/\s{1,200}/g, ' ');

}

// Example usage

var inputString = $Inputparameter

$outputparameter = removeExtraSpaces(inputString);

Example:

Inputparameter = "India  USA     China         UK" 

outputparameter  = "India USA China UK" 

Also refer to the oml provided.

Hope that helps.

Regards

Anees

ForumDemo.oml
2023-12-22 05-02-09
_N_C_
2025-03-11 05-02-41
Ragu Vasu

Hi @ZH T,

Use below JS function below and pass your input string as a parameter, it will return the Expected output. 

function removeSpaces(input) 

{

    return input.replace(/\s+/g, ' ');   // Use regular expression to replace Multiple spaces with a single space

}

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.