Open In App

JavaScript RegExp (x|y) Expression

Improve
Improve
Like Article
Like
Save
Share
Report

The RegExp (x|y) Expression in JavaScript is used to search any of the specified characters (separated by |). 

Syntax:

/(x|y)/ 

or

new RegExp("(x|y)")

Syntax with modifiers:

/(x|y)/g 

or

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

Example 1: This example searches the word “GEEKS” or “portal” in the whole string. 

Javascript




function geek() {
    let str1 = "GEEKSFORGEEKS is the computer "
        + "science portal for geeks.";
 
    let regex4 = /(GEEKS|portal)/g;
    let match4 = str1.match(regex4);
 
    console.log("Found " + match4.length
        + " matches: " + match4);
}
geek()


Output

Found 3 matches: GEEKS,GEEKS,portal

Example 2: This example searches the digit numbered 0 or 5 in the whole string. 

Javascript




function geek() {
    let str1 = "012034567895";
    let regex4 = /(0|5)/g;
    let match4 = str1.match(regex4);
 
    console.log("Found " + match4.length
        + " matches: " + match4);
}
geek()


Output

Found 4 matches: 0,0,5,5

Supported Browsers: The browsers supported by RegExp (x|y) Expression are listed below.

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  


Last Updated : 30 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads