Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Regex in JS

As we all know, regular expression is a sequence of characters that forms a search pattern. Here in JavaScript the search pattern can be used for text search and text replace operations in much powerful way.

SYNTAX: /pattern/modifiers;

Example: var patt = /adarsh/i;
Here, adarsh is a pattern and i is a modifier(represents make search case-insensitive)

We can combine REGEX with string methods like search() and replace() to make our operations easy.

Using search(): The search() method searches a string for a specified value and returns the position of the match.

Example: 
var str = "I'm Adarsh!";
var z = str.search("/adarsh/i");
Here, 4 will be stored in z as adarsh is presentin original string. Please note here we have used modifier i due to which Adarsh can search adarsh.

Using replace(): The replace() method replaces a specified value with another value in a string.

Example: 
var str = "I'm Adarsh";
var z = str.replace(/adarsh/i, "Harshdeep");
Here, I'm Harshdeep will be stored in z variable.

Some of the REGEX patterns are:

  • [abc]: Find any of the characters between the brackets.
  • [0-9]: Find any of the digits between the brackets.
  • (x|y): Find any of the alternatives separated with |.
  • \d: Find a digit.
  • \s: Find a whitespace character.
  • \b: Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b.
  • \uxxxx: Find the Unicode character specified by the hexadecimal number xxxx.

test(): We have a special feature in JavaScript of test() method. It searches a string for a pattern, and returns true or false, depending on the result.

Example: 
var patt = /e/;
patt.test("Sun and Moon are friend");
Since there is an "e" in the string, the output of the code above will be: true

Also please note we may use let new RegExp(pattern, “modifier”) to create our own REGEX with required pattern and modifier.

Try the following example in the editor below.

You are given a string Str and a pattern Patt, you have to search Patt in Str and print true if it is present otherwise print false. Make search case insensitive.

Sample Input:

adarsh
arsh

Sample Output:

true
Start solving Regex in JS on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test