Menu ▼


JavaScript replace() method will replace only the first occurrence of the string. If you want to replace all the occurrences of a string then you have to write your custom code. By using JavaScript regular expressions or by JavaScript array split and join we can replace the all occurrences of spaces, characters, commas or the given input. Lets see how to do that with below examples.

Replace all spaces in a string

var input:'Hello h o w Ar e you?'
input = input.replace(/ /g,'');

Output
//'HellohowAreyou?'


Replace all commas in a string

var input:'Hello, Welcome, to, dotnet,learners,.com'
input = input.replace(/,/g,'');

Output
//'Hello Welcome to dotnetlearners.com'


Replace all occurrences of specific word in a string
Below example will replace all occurrences of the word 'and'

var input:'Example and to and replace and all and the and occurrences and of and a and specific and word'
input = input.replace(/and/g,'');

Output
//'Example to replace all the occurrences of a specific word'


JavaScript function to replace all occurrences of a string


function replaceAll(input, replace, replacewith) {
    return input.replace(new RegExp(replace, 'g'), replacewith);
}

Usage
var input:'Example and to and replace and all and the and occurrences and of and a and specific and word'
replaceAll(input, 'and', '');


Replace all occurrences using JavaScript array split and join.


function replaceAll(input, replace, replacewith) {
    return input.split(replace).join(replacewith);
}

Usage
var input:'Example and to and replace and all and the and occurrences and of and a and specific and word'
replaceAll(input, 'and', '');


Note
Even in JQuery there is no replaceAll function to replace all the occurrences of a string. So you have to use any one of the above JavaScript approach.

Read More...
Submited By : mrkraju On 04 Dec 2015

The below JavaScript function will validate date for the following formats.
1. mm/dd/yyyy
2. mm/dd/yy
3. mm-dd-yyyy
4. mm-dd-yy

JavaScript function

//Checks date for mm/dd/yyyy, mm/dd/yy, mm-dd-yyyy and mm-dd-yy format.
function checkDate(inputdate) {
    if (inputdate == '')
        return false;

    var rxDatePattern = /^(0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-]((?:19|20)\d\d)$/

    var dtArray = inputdate.match(rxDatePattern);
    if (dtArray == null) {
        rxDatePattern = /^(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/]((?:19|20)\d\d)$/
        dtArray = inputdate.match(rxDatePattern);
        if (dtArray == null)
            return false;
    }

    dtMonth = dtArray[1];
    dtDay = dtArray[2];
    dtYear = dtArray[3];

    if (dtMonth < 1 || dtMonth > 12)
        return false;
    else if (dtDay < 1 || dtDay > 31)
        return false;
    else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
        return false;
    else if (dtMonth == 2) {
        var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
        if (dtDay > 29 || (dtDay == 29 && !isleap))
            return false;
    }
    return true;
}

Example
date validation in javascript

Example source code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script language="javascript">
        //Checks date for mm/dd/yyyy, mm/dd/yy, mm-dd-yyyy and mm-dd-yy format.
        function checkDate(inputdate) {
            if (inputdate == '')
                return false;

            var rxDatePattern = /^(0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-]((?:19|20)\d\d)$/

            var dtArray = inputdate.match(rxDatePattern);
            if (dtArray == null) {
                rxDatePattern = /^(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/]((?:19|20)\d\d)$/
                dtArray = inputdate.match(rxDatePattern);
                if (dtArray == null)
                    return false;
            }

            dtMonth = dtArray[1];
            dtDay = dtArray[2];
            dtYear = dtArray[3];

            if (dtMonth < 1 || dtMonth > 12)
                return false;
            else if (dtDay < 1 || dtDay > 31)
                return false;
            else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
                return false;
            else if (dtMonth == 2) {
                var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
                if (dtDay > 29 || (dtDay == 29 && !isleap))
                    return false;
            }
            return true;
        }

        function validatedate() {
            if (checkDate(document.getElementById("txtDate").value)) {
                document.getElementById("divmsg").innerHTML = "Valid Date.";
                document.getElementById("divmsg").style.color = "green";
            }
            else {
                document.getElementById("divmsg").innerHTML = "Invalid Date.";
                document.getElementById("divmsg").style.color = "red";
            }
        }

    </script>
</head>
<body>
    Date :
    <input id="txtDate" type="text" value="11/19/2015">
    <input type="button" value="Validate" onclick="validatedate();" />
    <br />
    <br />
    <div id="divmsg">
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 19 Nov 2015

This example will shows you how to replace single quote form a string. Default javascript replace method will replace the first occurrence of the single quote, to replace all the occurrences we have to use regular expression.

  • Removing all the single quotes from a string.
    var outputstr= inputstring.replace(/'/g,'');

  • Replacing all the single quotes with double quote in a string.
    var outputstr= inputstring.replace(/'/g,'"');


Example:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script language="javascript">
        function RemoveSingleQuotes() {
            var outputstr = $("#txtinput").val().replace(/'/g, '');
            alert(outputstr);
        }
        function ReplaceSingleQuotes() {
            var outputstr = $("#txtinput").val().replace(/'/g, '"');
            alert(outputstr);
        }    
    </script>
</head>
<body>
    <div>
        <input type="text" id="txtinput" />&nbsp;&nbsp;
        <input type="button" value="Remove Single Quotes" onclick="RemoveSingleQuotes();" />&nbsp;&nbsp;
        <input type="button" value="Replace Single Quotes" onclick="ReplaceSingleQuotes();" />
    </div>
</body>
</html>
Read More...
Submited By : mrkraju On 30 Apr 2015

    <script language="javascript">

        String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, ""); }   

    </script>

 Usage :

alert("    test   ".trim());

Read More...
Submited By : mrkraju On 03 Dec 2013




Search dotnetlearners.com