JavaScript replace tab characters

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

This wiki is for replacing tab characters in a string with a different character using JavaScript. In this recipe, we use a pipe (|) to replace the tab.

[edit] code

<html>
<head>
<title></title>
</head>
<body>
<form name="form1">
    <input type="textbox" name="txtInput" />
    <script type="text/javascript">
    function validate() {
        if (! document.form1.txtInput.value.match(/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,15}$/)) {
            alert("Please enter valid value!")
        } else {
            alert("Success!")
        }
    }
    </script>
    <input type="button" name="btnSubmit" onclick="validate()" value="Go" />
</form>
</body>
</html>

[edit] How It Works

Breaking the recipe down yields simply the following:

Regular Expression Description
\t is a tab, replaced by . . .
| a pipe character.

By default, the regular expression object in the .NET Framework replaces each occurrence of the text that matches the search regex. However, the VBScript and JavaScript regular expres- sion objects work differently because an option must be specified to replace each match. You can find more about these options in the “Syntax Overview” section of this book.

[edit] Variations

Since this is such a simple recipe, it has an extensive number of variations. You could replace the character class representing a tab with other character classes, especially the \s character class. The JavaScript variation /\s/g would replace each instance of whitespace with |.

One variation on the previous recipe is to use a qualifier after the character class to replace more than one instance of a tab at once. For instance, if you want to replace two tabs, you could use something such as /\t{2}/g in the JavaScript recipe.

Personal tools