How to make pattern in regex in javascript

Regular expression flag pattern match() test()

Regular Expression represented by regex or regexp is a powerful pattern tool to work upon string or text. It makes a pattern to work on strings to search or manipulate specific strings or text. Even if we can select characters, words and also whole strings of a text document.

They are commonly used in text editors, and programming languages for searching, replacing or data validation.

Features of regex

  1. Search and replace

    To make changes in text documents regex is used to find specific text and replace them with another text.

  2. Data-validation

    Regex patterns decide whether that text is in a specific way or not. For example, the email id must contain a '@' sign and the phone number must be in number format.

  3. Data-cleaning

    Regex removes unwanted data like messy content from the documents.

syntax

The most important syntax of the regex is /pattern/flag

pattern

It is a part of the text which is used to find specific text. For example, if we want to find 'PW skills' in any text document, we have to make a pattern. let pattern=/PW skills/;

flag

flags are used to give specific permissions to find text in the document. For example, /i is used to ignoring case sensitivity. /g(global) is used for multiple matches. If you want to know more about flags visit https://regexr.com/

test()

The test() method is a method of Regex object to check if a string matches a given regular expression.

match()

This object method is used to find all matches of a regular expression in a string.

Thus, we find that match() and test() methods are used by their requirements.

Example

Now we want to find numbers in any string.

output:

Did you find this article valuable?

Support WebIndia by becoming a sponsor. Any amount is appreciated!