How To Use Regular Expressions In JavaScript

Published Date Author: , Posted November 3rd, 2011 at 4:03:13pm

RegExp.exec(string)

Applies the RegExp to the given string, and returns the match information.

match then contains ["Sample","amp"]

RegExp.test(string)

Tests if the given string matches the Regexp, and returns true if matching, false if not.

match then contains false

String.match(pattern)

Matches given string with the RegExp. With g flag returns an array containing the matches, without g flag returns just the first match or if no match is found returns null.

str then contains ["o","or","ro"]

String.search(pattern)

Matches RegExp with string and returns the index of the beginning of the match if found, -1 if not.

ndx then contains 10

String.replace(pattern,string)

Replaces matches with the given string, and returns the edited string.

str then contains "Eric said: My name is Eric!"

String.split(pattern)

Cuts a string into an array, making cuts at matches.

str then contains ["I","am","Eric"]

No comments as yet.

Leave Your Comment  Leave a comment

All fields marked with "*" are required.