SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.






Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space


In this Javascript tutorial, web developers will find an example Javascript code that they can use in order to replace and remove multiple spaces with single space character in a given text or string variable.
Multi space characters in text and string variables can be a big problem not only for Javascript developers but also in VB.NET, ASP.NET, T-SQL or in all other programming languages.

Javascript String REPLACE function has two arguments.
The first argument or the first parameter used in Javascript Replace function is string to be replaced.
The second argument is the new replace string.
For more detailed information String Replace function, please refer to Javascript Replace Function and String Replace Method.

In this Javascript Replace function example, we will remove the redundant multi space characters in the string variable text :
"Replace     multiple  spaces with single    space      using javascript replace function"





Here is the variable declarations part of our Javascript Replace function example code.
Note that there exist multi space characters between words in out example Javascript string variable.

<script type="text/javascript" type="text/javascript">
var string_variable;
string_variable = "Replace     multiple  spaces with single    space      using javascript replace function";
alert(string_variable);
</script>

string-functions-javascript-replace-function-example

Now, we will search for two spaces next to eacj other as " " in the javascript string variable.
If we can find at least one, we will replace two adjacent space characters with single space character in a Javascript While loop.
Let's build the While Loop in Javascript code and replace and remove multiple spaces with one space in a recursive method.

<script type="text/javascript" type="text/javascript">
var string_variable;
string_variable = "Replace     multiple  spaces with single    space      using javascript replace function";
alert(string_variable);

var intIndexOfMatch = string_variable.indexOf("  ");

while (intIndexOfMatch != -1){
  string_variable = string_variable.replace( "  ", " " )
  intIndexOfMatch = string_variable.indexOf( "  " );
}

alert(string_variable);
</script>

Here is the output of our example Javascript Replace function in string.

javascript-replace-function-in-string

As you can see, the Javascript string replace function we code, remove multiple spaces with single space character.






Javascript

Javascript Articles

Javascript Forums












Javascript Tutorials, Articles and Javascript Codes

Morris Number Sequence Puzzle Solution in Javascript
Javascript Array Puzzle: People with Sword Around a Circle
Sort Array in Random Order using Javascript Shuffle Function
Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums




Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems