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

Finding Matches

Finding matches is all about searching text. You create a regular expression, also called a pattern, and then see whether that pattern matches any of the text you’re searching. You can use regular expressions in JavaScript to find matches in several ways:

  • The String object’s match method. This method finds regular expression matches in a string.

    Syntax: string.match(regExp). Returns an array of matches or null if there were no matches. See Chapter 18, “The Date, Time, and String Objects,” for more information.

  • The String object’s search method. This method returns the position of the first substring match in a regular expression search.

    Syntax: string.search(regExp), where regExp is the regular expression to match. See Chapter 18 for more information.

  • The regular expression’s exec method. Like the String object’s match method, this method finds regular expression matches in a string. Syntax: regularExpression.exec(string), where string is the string you’re searching. Returns an array of matches or null if there were no matches.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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