JavaScript Find a Word

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

You can use this recipe for finding single words in a block of text. The expression will find only complete words surrounded by spaces or other word delimiters, such as punctuation or the beginning or end of a line.

[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(/\bsomething\b/)) {
            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

A special character class, \b, allows you to easily search for whole words. This is an advantage because without doing a whole bunch of extra work you can make sure that a search for some- thing, for example, doesn’t yield unexpected matches such as somethings. You can break the regular expression shown here into the following:

Regular Expression Description
\b a word boundary (a space, beginning of a line, or punctuation) . . .
something s, o, m, e, t, h, i, n, and g . . .
\b a word boundary at the end of the word.

This expression differs just slightly from the C# and Visual Basic .NET examples because the RegularExpressionValidator control assumes that the expression is to match the entire value (there’s an implied ^ at the beginning of the expression and $ at the end of the expres- sion). The combination .* has been added before and after the word boundary \b so the full word can float around inside the line.

Personal tools