Users Online

· Guests Online: 45

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Functions - Regular Expressions in JavaScript

Regular Expressions in JavaScript

Regular Expressions in Java Script

Introduction to Regular Expressions in JavaScript

The following article provides an outline for Regular Expressions in JavaScript. Regular expressions are a string of characters that are used to validate the contents of another string. These strings of characters that form the regular expression are stored in an object.

Syntax:

var regEx = /^[A-Za-z]/;

Here the variable object named “regEx” holds the regular expression pattern.

Now let’s test if another string matches this regular expression.

Code:

var str = "EduCBA";
var regEx = /^[A-Za-z]/;
var res = "false";
if(str.match(regEx)){
res= "true";
}
alert(res);

Output:

true

Here in the above example, the regular expression checks whether a string contains only alphabets A through Z in both upper and lower cases. If it does, it returns “true”, if not “false”.

Regular Expression Syntax in JavaScript

A regular expression consists of two parts. The first part is the pattern, which is followed by an optional flag.

Syntax:

var regEx = /pattern/flag

Flags are also referred to as modifiers.

Few commonly used optional flags are:

1. g – global

Finds multiple matches. If not used, it stops after the first match.

Code

var str = "I scream, you scream, we all scream for ice cream";
var regEx = / scream/g;
var result = str.match(regEx);
alert( result );

Output:

scream, scream, scream // It returns all the matches in the string.

2. i – ignore-case

This is case-insensitive and matches both upper and lower cases. If not set, then the search is case-sensitive.

Code

var str = "Hello EduCBA";
var regEx = /educba/i;
alert( str.search(regEx) );

Output:

6 //returns the index at which the string is found.

alert( str.search(/educba/) ); //without global flag

Output:

-1

3. m – multi-line

It affects the behavior of characters “^” and “$”. In the case of multi-line, it looks for matches at the start and end of each line rather than that of the entire string. If it isn’t in multi-line mode, then only the matches from the entire string are returned.

Code

var str = `I scream,
you scream,
we all scream for ice cream`;
var regEx = /^\w+/gm;
var result = str.match(regEx);
alert( result );

Output:

I,you,we //prints the first word of every line.

Now, let us take a look at the patterns in the regular expression. The pattern consists of ranges, metacharacters, quantifiers, etc.

Metacharacters of Regular Expressions in JavaScript

The metacharacters form the base of the regular expression pattern. These are a combination of backward slash with an alphabetical character which together form a metacharacter, and each of them has a special meaning associated with each of them. For example, “\n” denotes a new line.

A few other examples of metacharacters are as follows:

  • \t – Used to find a tab character.
  • \v – Used to find a vertical tab character.
  • \s – Used to find white space characters.
  • \S – Used to find non-whitespace characters.
  • \d – Used to find numerical digits.
  • \D – Used to find nun – numeric digits.
  • \w – Used to find words.
  • \W – Used to find anything except for words.
  • . – A dot is used to find a single character other than the new line or end of the line.
  • \0 – Used to find a null character.

Sets and Ranges of Regular Expressions in JavaScript

In this scenario, square brackets are used as a part:

Syntax:

Say, for the set [abc] – only the characters a, b and c are to be considered.

Example #1:

  • [^abc] – That is any character other than abc.
  • [a|b] – Either a or b can be considered.

A range is provided between that range; all the characters are to be considered. For example: [a-z] means all characters from a through z in lower case will be taken into consideration.

Example #2:

  • [0-9] – All characters from zero through nine.
  • [A-Z] – All characters from A to Z in upper case.

Quantifiers of Regular Expressions in JavaScript

These are denoted with the help of special characters. Each special character has a meaning associated with it. These characters are used along with regular expressions.

A few of the most used quantifiers are:

  • * – Matches a string containing zero or more instances.
  • + – Matches a string containing one or more instances.
  • ? – Matches a string containing zero or one instance.
  • {n} – Here, “n” takes in a number. Matches the required regular expression the number of times mentioned in place of “n”.
  • $ – Matches the given expression with the end of the string.
  • ^ – Matches the given expression with the beginning of the string.
  • ?= – Matches any string with the regex pattern after the equals sign.
  • ?! – Matches any string that does not contain the regex pattern after the exclamatory sign.

Properties of Regular Expressions in JavaScript

It consists of the following properties:

  • Constructors – Determines which function has created the regular expression object prototype.
  • Global – Checks whether the flag “g” is set.
  • Ignore case – Check whether the flag “i” has been set.
  • The last Index – Determines the index position at which the next match needs to start from.
  • Multiline – Checks whether the flag “m” has been set.
  • Source – Gives the text of the regular expression pattern.

Methods in Regular Expressions and Strings

Commonly used methods in regular expressions are as follows:

  • exec() – Executes to look for matches in a string and returns the first match. If no match is found, then it returns null.
  • test() – Tests for matches in a string and returns the result in the form of Boolean. That is, if a match is found, it returns true; if not, then it returns false.
  • toString() – Returns an equivalent string object of the regular expression.

String methods are useful when working with regular expressions.

  • match() – It’s a String method that looks for a match in a string. If found, returns the match; if not, then returns null.
  • matchAll() – It’s a String method that looks for all the matches in a string.
  • split() – It’s a string method used to break a string based on the regular expression provided.
  • Search () – It’s a string method used to look for a match against the given regular expression. Returns 0 on success and -1 on failure.

Conclusion

Regular expressions can be written by making use of different combinations of metacharacters, quantifiers, and flags whenever required. They also come with their own set of methods and also work well with commonly used string methods. One could easily use them for validation or content restriction as per one’s requirement.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.74 seconds
10,841,155 unique visits