JavaScript regular expression tutorial for beginner

A javascript regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text. Lets start learning javascript regular expression

Syntax

var pattern = new RegExp(pattern, modifier);
OR
/pattern/modifier;

Here is the description of the parameters −

  • Pattern − A string that specifies the pattern of the regular expression or another regular expression.
  • Modifiers − An optional string containing any of the “g”, “i”, and “m” attributes that specify global, case-insensitive, and multi-line matches, respectively.

Example

var pattern = /w3path/i;

Example explained:

/w3path/i  is a regular expression.

w3path  is a pattern

i  is a attributes (for case-insensitive).

Regular Expression Modifiers

Modifiers can be used to perform case-insensitive more global searches:

ModifierDescription
iPerform case-insensitive matching
gPerform a global match (find all matches rather than stopping after the first match)
mPerform multiline matching

Brackets

Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find a range of characters.

ExpressionDescription
[…]Any one character between the brackets.
[abc]Find any of the characters between the brackets.
[^abc]Find any character NOT between the brackets
[0-9]Find any of the digits between the brackets
(x|y)Find any of the alternatives separated with |

Metacharacters are characters with a special meaning:

MetacharacterDescription
\dFind a digit
\sFind a whitespace character
\bFind a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
\uxxxxFind the Unicode character specified by the hexadecimal number xxxx

Quantifiers define quantities:

QuantifierDescription
n+Matches any string that contains at least one n
n*Matches any string that contains zero or more occurrences of n
n?Matches any string that contains zero or one occurrences of n
n{X}Matches any string that contains a sequence of X n‘s
n$Matches any string with n at the end of it
^nMatches any string with n at the beginning of it
?=nMatches any string that is followed by a specific string n
?!nMatches any string that is not followed by a specific string n

Examples

Following examples explain more about matching characters.

Expression Description
[^a-zA-Z]It matches any string not containing any of the characters ranging from a through z and A through Z.
p.pIt matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$It matches any string containing exactly two characters.
<b>(.*)</b>It matches any string enclosed within <b> and </b>.
p(hp)*It matches any string containing a p followed by zero or more instances of the sequence hp.

RegExp Properties

PropertyDescription
constructorSpecifies the function that creates an object’s prototype.
globalSpecifies if the “g” modifier is set.
ignoreCaseSpecifies if the “i” modifier is set.
lastIndexThe index at which to start the next match.
multilineSpecifies if the “m” modifier is set.
sourceThe text of the pattern.

RegExp Methods

Here is a list of the methods associated with RegExp along with their description.

Method Description
exec()Executes a search for a match in its string parameter.
test()Tests for a match in its string parameter.
toSource()Returns an object literal representing the specified object; you can use this value to create a new object.
toString()Returns a string representing the specified object.

Real life example of the JavaScript regular expression

<script> 


// only string 
var expr = new RegExp(/[a-zA-Z]/)

expr.test('ab') 
// Output: true
  
</script>

Recommended Posts

Javascript Promise

One Comment

  1. Titus Ferard said:

    First time visiting your website, I really like your website!

    April 22, 2020
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *