Article

Regular Expressions in JavaScript

Page: 1 2 3 4 5

search()

The search() function is similar to the well-known indexOf() function, except it takes a regular expression instead of a string. It then searches the string for the first match to the given regular expression and returns an integer indicating the position in the string (e.g. 0 if the match is at the start of the string, 9 if the match begins with the 10th character in the string). If no match is found, the function returns a value of –1.

Summing it up

Regular expressions are an invaluable tool for verifying user input. By taking advantage of support for regular expressions in JavaScript, that verification can be done without having to resort to complex and potentially costly server-side scripting. In fact, you can make your server-side scripts considerably simpler if you verify user input with JavaScript before allowing that data to be submitted, since your server-side scripts can assume that the data it receives is valid. Simpler scripts run faster, and lighten the load on your Web server.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: