Range

Introduction

A range represents a contiguous sequence of characters.

Form

A range indicates that all characters greater than or equal to the starting character and less than or equal to the ending character will be matched.

[STARTING_CHARACTER-ENDING_CHARACTER]

Use

Example

The pattern "[a-z]" matches any character in the range "a" to "z".

var pattern = /[a-z]/; var string = "abcdabc"; document.writeln(string.match(pattern));

On running the above code, the following output is obtained.

Example

The pattern "[^A-Z]" matches any character other than those in the range "A" to "Z".

var pattern = /[^A-Z]/; var string = "abcdabc"; document.writeln(string.match(pattern));

On running the above code, the following output is obtained.

Example

The pattern "[b-df-hj-np-tv-z]" matches any consonant.

var pattern = /[b-df-hj-np-tv-z]/; var string = "aeioukaeiou"; document.writeln(string.match(pattern));

On running the above code, the following output is obtained.

Example

The pattern "0[1-9]|1[0-2]" matches any number between 01 and 12 written with two digits.

var pattern = /0[1-9]|1[0-2]/; var string = "aeiou12aeiou"; document.writeln(string.match(pattern));

On running the above code, the following output is obtained.