Article
Regular Expressions in JavaScript
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.