• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Matching Words

It’s worth taking a look at a common task—matching whole words—because there are several ways to do that. For example, you can match a word using \S, which matches non-white space characters. Here’s an example that does that, matching and displaying the first word in some text:

<HTML> 
    <HEAD> 
        <TITLE>Finding Words</TITLE> 
        <SCRIPT LANGUAGE="JavaScript"> 
            <!--
               function displayer() 
               { 
                    var regexp = /\S+/
					var matches = document.form1.text1.value.match(regexp)
					document.form1.text2.value = matches[0] 
            } 
          //--> 
        </SCRIPT> 
    </HEAD> 
    <BODY> 
        <H1>Finding Words</H1> 
        <FORM NAME="form1"> 
            <INPUT TYPE="TEXT" NAME="text1" VALUE="JavaScript is the subject." SIZE="30"> 
            <BR> 
            <INPUT TYPE="BUTTON" VALUE="Find First Word" ONCLICK="displayer()"> 
            <BR> 
            <INPUT TYPE="TEXT" NAME="text2"> 
        </FORM> 
    </BODY> 
</HTML> 

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint