WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Regular Expressions


A standard articulation is a succession of characters that frames a look pattern.

The scan example can be utilized for content hunt and content supplant tasks.


What Is a Regular Expression?

A standard articulation is an arrangement of characters that frames a search pattern.

When you scan for information in a content, you can utilize this hunt example to portray what you are looking for.

A standard articulation can be a solitary character, or an increasingly confounded pattern.

Regular articulations can be utilized to play out a wide range of text search furthermore, text replace operations.

Syntax

/pattern/modifiers;

Example

var patt =/welookups/i;

Example explained:

/welookups/i  is a standard expression.

welookups  is an example (to be utilized in a search).

i  is a modifier (changes the inquiry to be case-insensitive).


Using String Methods

In JavaScript, ordinary articulations are regularly utilized with the two string methods: seek() and replace().

The look() method utilizes an articulation to scan for a match, and returns the situation of the match.

The supplant() method restores an adjusted string where the design is replaced.


Using String seek() With a Regular Expression

Example

Use a standard articulation to complete a case-coldhearted scan for "welookups" in a string:

var str = "Visit welookups";
var n = str.search(/welookups/I);

The result in n will be:

6
Try it Yourself »

Using String seek() With String

The look strategy will likewise acknowledge a string as pursuit contention. The string contention will be changed over to an ordinary articulation:

Example

Use a string to complete a look for "welookups" in a string:

var str = "Visit welookups!";
var n = str.search("welookups");
Try it Yourself »

Use String supplant() With a Regular Expression

Example

Use a case obtuse customary articulation to supplant Microsoft with welookups in a string:

var str = "Visit Microsoft!";
var res = str.replace(/microsoft/I, "welookups");

The result in res will be:

Visit welookups!
Try it Yourself »

Using String supplant() With a String

The supplant() strategy will likewise acknowledge a string as hunt contention:

var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "welookups");
Try it Yourself »

Regular Expression Modifiers

Modifiers can be utilized to perform case-harsh increasingly worldwide searches:

Modifier Description
i Perform case-harsh matching
g Perform a worldwide match (discover all matches instead of ceasing after the first match)
m Perform multiline matching

Regular Expression Patterns

Brackets are utilized to discover a scope of characters:

Expression Description
[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 options isolated with |

Metacharacters are characters with an uncommon meaning:

Metacharacter Description
\d Find a digit
\s Find a whitespace character
\b Find a match toward the start or toward the finish of a word
\uxxxx Find the Unicode character indicated by the hexadecimal number xxxx

Quantifiers characterize quantities:

Quantifier Description
n+ Matches any string that contains something like one n
n* Matches any string that contains at least zero events of n
n? Matches any string that contains zero or one events of n

Using the RegExp Object

In JavaScript, the RegExp object is a normal articulation object with predefined properties and methods.


Using test()

The test() strategy is a RegExp articulation method.

It looks a string for an example, and returns genuine or on the other hand false, contingent upon the result.

The following model scans a string for the character "e":

Example

var patt =/e/;
patt.test("The best things in life are free!");

Since there is a "e" in the string, the yield of the code above will be:

genuine
Try it yourself »

You don't need to put the customary articulation in a variable first. The two lines above can be abbreviated to one:

/e/.test("The best things in life are free!");

Using exec()

The executive() technique is a RegExp articulation method.

It scans a string for a predefined example, and returns the discovered content.

If no match is discovered, it returns null.

The following model looks a string for the character "e":

Example 1

/e/.exec("The best things in life are free!");

Since there is a "e" in the string, the yield of the code above will be:

e
Try it yourself »