THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript RegExp (x|y) Expression

RegExp Object Reference JavaScript RegExp Object

Example

Do a global search to find any of the specified alternatives (red|green):

var str = "re, green, red, green, gren, gr, blue, yellow";
var patt1 = /(red|green)/g;

The marked text below shows where the expression gets a match:

re, green, red, green, gren, gr, blue, yellow
Try it Yourself »

Definition and Usage

The (x|y) expression is used to find any of the alternatives specified.

The alternatives can be of any characters.


Browser Support

Expression
(x|y) Yes Yes Yes Yes Yes

Syntax

new RegExp("(x|y)")

or simply:

/(x|y)/

Syntax with modifiers

new RegExp("(x|y)","g")

or simply:

/\(x|y)/g

More Examples

Example

Do a global search to find any of the specified alternatives (0|5|7):

var str = "01234567890123456789";
var patt1 = /(0|5|7)/g;

The marked text below shows where the expression gets a match:

01234567890123456789
Try it Yourself »

RegExp Object Reference JavaScript RegExp Object