JavaScript – Regular Expression


What is Regular expression ?

Regular expression is a sequence of symbols and characters expressing a string / pattern to be searched for within a longer piece of text.

What is Regular expression in programming language ?

Regular expression is mentioned in short as “regex”.

“regex” is a string of text that allows us to create patterns for matching, locating, and managing text.

What is Regex in Javascript ?

Regex is an javascript built-in object which describes a pattern of charecters or text.

Regex is a sequence of characters forms a search pattern.

Regex can be either a single character, or in more complicated patterns.

Why Regex is used in javascript ?

Regex is used to perform pattern-matching in the long text.

Regex also allows us to perform and “search-and-replace” functions on the whole text.

Search patterns in Regex is used for searching text and replacing text operations.

Best javascript tutorial !!!

Click to Learn More about – Javascript online learning

Syntax :

/pattern/modifiers;

pattern is used in a search.


modifier modifies the search to be Case-Insensitive

Note :

Modifiers can be used to perform multiline searches.

There are three important terms in Regular expression, they are

1. Modifiers

2. Patterns

3. Quantifiers

1. Modifiers

There are list of modifiers used in Regex,

1. [abc]

To find any of the character inside the brackets

2. [0-9]

To find any of the digits between the brackets 0 to 9

3. (x | y)

To find any of the alternatives between x or y separated with |

2. Patterns

1. \d

This is used to find a digit

2. \s

This is used to find a whitespace character

3. \b

This is used to find a match at beginning or at the end of a word

4. \uxxxx

This is used to find the Unicode character specified by the hexadecimal number xxxxx

3. Quantifiers

1. n+

This is used to match any string that contains at least one n

2. n*

This is used to match any string that ccontains zero or more occurrences of n

3. n?

This is used to matches any string that contains zero or one occurrences of n

Regular expression can be acheived by using two string methods.

1. search() method

2. replace() method

1. search() method

By using search() method we can search specific text and returns the position of the text in the long tailed text.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
		var str = "Kalam earned a degree in aeronautical engineering from the Madras Institute of Technology and in 1958 joined the Defence Research and Development Organisation (DRDO).";  
		var SearchString = str.search(/research/i);  
		var SearchString1 = str.search(/Technology/); 
		var SearchString2 = str.search(/aeronautical/); 
		var SearchString3 = str.search(/Madras/); 
		var SearchString4 = str.search(/Defence/); 
		document.write("The position of the String 'research' in str = "+SearchString + '<br>'); 
		document.write("The position of the String 'technology' in str = "+SearchString1 + '<br>'); 
		document.write("The position of the String 'aeronautical' in str = "+SearchString2 + '<br>'); 
		document.write("The position of the String 'madras' in str = "+SearchString3 + '<br>'); 
		document.write("The position of the String 'defence' in str = "+SearchString4 + '<br>'); 
		</script>
	</head>
	<body></body>
</html>

Output :

The position of the String ‘research’ in str = 121


The position of the String ‘technology’ in str = 79

The position of the String ‘aeronautical’ in str = 25

The position of the String ‘madras’ in str = 59

The position of the String ‘defence’ in str = 113

2. replace() method

By using replace() method we can replace the matched pattern of text with the new text inside the long tailed text.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
		var str = "He earned a degree in aeronautical engineering from the Madras Institute of Technology and in 1958 joined the Defence Research and Development Organisation (DRDO).";  
		var SearchString = str.replace('He','Abdul Kalaam');  
		var SearchString1 = str.replace('aeronautical','Chemical');  
		document.write("'He' is replaced with the Name 'Abdul Kalaam' in str, modified new str = "+SearchString + '<br>'); 
		document.write("'aeronautical' is replaced with the Name 'Chemical' in str, modified new str = "+SearchString1 + '<br>'); 
		</script>
	</head>
	<body></body>
</html>

Output :

‘He’ is replaced with the Name ‘Abdul Kalaam’ in str, modified new str = Abdul Kalaam earned a degree in aeronautical engineering from the Madras Institute of Technology and in 1958 joined the Defence Research and Development Organisation (DRDO).’aeronautical’ is replaced with the Name ‘Chemical’ in str, modified new str = He earned a degree in Chemical engineering from the Madras Institute of Technology and in 1958 joined the Defence Research and Development Organisation (DRDO).