Javascript Regular Expression - Metacharacters (Examples)

Tech:
Javascript
Since:
1 year ago
Views:
8

Metacharacters in Javascript Regular Expression is a collection of characters that are used in regular expressions.

1: .

To find a single character, except newline or line terminator.

var str = "That's hot!";
var patt = /h.t/g;
var x = str.match(patt);

// OUTPUT - hat,hot

2: \w

To find a word character. A word character is from a-z, A-Z, 0-9, and _.

var str = "Give 100%!";
var patt = /\w/g;
var x = str.match(patt);

// OUTPUT - G,i,v,e,1,0,0

3: \W

To find a non-word character. A non-word character is other than a-z, A-Z, 0-9, and _.

var str = "Give 100%!";
var patt = /\W/g;
var x = str.match(patt);

// OUTPUT - ,%,!

4: \d

To find a digit. A digit is from 0-9.

var str = "Give 100%!";
var patt = /\d/g;
var x = str.match(patt);

// OUTPUT - 1,0,0

5: \D

To find a non-digit character.

var str = "Give 100%!";
var patt = /\D/g;
var x = str.match(patt);

// OUTPUT - G,i,v,e, ,%,!

6: \s

To find a whitespace character. A whitespace character is from space, tab, carriage return, newline, vertical tab, and form feed.

var str = "Is this all there is?";
var patt = /\s/g;
var x = str.match(patt);

// OUTPUT - , , ,

7: \S

To find a non-whitespace character. A non-whitespace character is other than space, tab, carriage return, newline, vertical tab, and form feed.

var str = "Is this all there is?";
var patt = /\S/g;
var x = str.match(patt);

// OUTPUT - I,s,t,h,i,s,a,l,l,t,h,e,r,e,i,s,?

8: \b

To find a match at the beginning/end of a word. If no match is found, it returns null. TO find a match at the beginning of a word, use \bSEARCHTERM. To find a match at the end of a word, use SEARCHTERM\b.

var str = "HELLO, LOOK AT YOU!";
var patt = /\bLO/;
var x = str.search(patt);

// OUTPUT - 7

var str = "HELLO, LOOK AT YOU!";
var patt = /LO\b/;
var x = str.search(patt);

// OUTPUT - 3

9: \B

To find a match, but not at the beginning/end of a word.

var str = "HELLO, LOOK AT YOU!";
var patt = /\BLO/;
var x = str.search(patt);

// OUTPUT - 3

var str = "HELLO, LOOK AT YOU!";
var patt = /LO\B/;
var x = str.search(patt);

// OUTPUT - 7

10: \0

To find a NULL character. If no match is found, it returns null.

var str = "Fallinfo \0";
var patt = /\0/;
var x = str.search(patt);

// OUTPUT - 9

11: \n

To find a new line character.

var str = "Fallinfo \n Fallinfo";
var patt = /\n/;
var x = str.search(patt);

// OUTPUT - 9

12: \f

To find a form feed character.

var str = "Fallinfo \f Fallinfo";
var patt = /\f/;
var x = str.search(patt);

// OUTPUT - 9

13: \r

To find a carriage return character.

var str = "Fallinfo \r Fallinfo";
var patt = /\r/;
var x = str.search(patt);

// OUTPUT - 9

14: \t

To find a tab character.

var str = "Fallinfo \t Fallinfo";
var patt = /\t/;
var x = str.search(patt);

// OUTPUT - 9

15: \v

To find a vertical tab character.

var str = "Fallinfo \v Fallinfo";
var patt = /\v/;
var x = str.search(patt);

// OUTPUT - 9

16: \OCATALNUMBER

To find the character specified by an octal number.

var str = "Fallinfow";
var patt = /\106/g;
var x = str.match(patt);

// OUTPUT - F

17: \xHEXNUMBER

To find the character specified by a hexadecimal number.

var str = "Fallinfo";
var patt = /\x46/g;
var x = str.match(patt);

// OUTPUT - F

18: \uUNICODE

To find the Unicode character specified by a hexadecimal number (Unicode only).

var str = "Fallinfo";
var patt = /\u0046/g;
var x = str.match(patt);

// OUTPUT - F