search() and match()

Let's see some examples of using the methods search() and match(). First, you create a string object.

var s = new String('HelloJavaScriptWorld');

Using match() you get an array containing only the first match:

s.match(/a/);

["a"]

Using the g modifier, you perform a global search, so the result array contains two elements:

s.match(/a/g);

["a", "a"]

Case insensitive match:

s.match(/j.*a/i);

["Java"]

The search() method gives you the position of the matching string:

s.search(/j.*a/i);

5
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +