Regular expressions are used to perform pattern matching and search, replace, etc. functions on the text and perform validation.
Extracting the data from text and sentences or validating those texts to any specific pattern is important in programming. The javascript has many regular expressions (patterns) that will make the programming easy.
Here is the general syntax for Regex,
SYNTAX:
/pattern/modifiers;
EXAMPLE:
Here is the example for the general syntax of the Regex,
/SpeakingEnglish/i;
- Using string search() with a regex in javascript.
To perform case insensitive searching in a string. And it searches for the position of the character.
It will search for the specified value of the string and will return the matched position of that string.
Here is an example for searching the string that starting with a capital letter:
let reg = "They speak English at work";
let result = reg.search(/English/i);
console.log(result);
OUTPUT : 11
Here is an example for searching the string starting with the small letter:
let reg = "They are speaking Spanish at work";
let result = reg.search(/english/i);
console.log(result);
OUTPUT : 11
- Using the test() method with a regex in javascript
The Test() method searches a string for a pattern, and after checking the result it will return true or false. The result depends on the result of the checking.
Here is an example for searching a string for the character "s":
let reg = "They speak English at work";
let pattern = /s/;
let result = pattern.test(reg);
console.log(result)
OUTPUT : true
let reg = "They speak English at work";
let pattern = /z/;
let result = pattern.test(reg);
console.log(result)
OUTPUT : false
- Using string replace() with a regex in javascript.
Replace() regular expression perform the replacing of a specific value with another value.
If we want to change a value with a specific value, we can use this method.
Here is an example of this:
let reg = "They speak English at work";
let result = reg.replace(/english/i,"Hindi");
console.log(result)
OUTPUT: "They speak Hindi at work"
REGEX MODIFIERS IN JAVASCRIPT
In javascript, we use many modifiers in regular expressions to make the programming easy.
Here are some common javascript modifiers like Case insensitive,multi-line, global, etc.
Modifiers | Description | Examples |
I | To match case insensitive | let reg = "VisitCybrosys Technologies"; let result =reg.match(/cybrosys/i); console.log(result) OUTPUT: //[object Array] (1) ["Cybrosys"] |
M | Matching in the multiline. | let reg = "\nIn th\nin that?";let result = reg.match(/^in/m);console.log(result)OUTPUT:// [object Array] (1)["in"] |
G | Match all (globally) after first matching it will not stop. | let reg = "minute to minute";let result = reg.match(/minute/g);console.log(result)OUTPUT:// [object Array] (2)["minute","minute"] |
REGEX PATTERNS IN JAVASCRIPT
Here explains some common patterns in the javascript like Brackets, Metacharacters, Quantifiers, etc.
BRACKETS
Bracket modifiers are used to find the range of characters in the text. Many bracket modifiers are in javascript regular expressions. Here explain some common bracket modifiers.
Expression | Description | Example |
(0-9) | | Digits between brackets: | This type of expression modifier returns the numbers between the bracket. let reg = "0123456789";let result = reg.match(/[2-5]/g);console.log(result)OUTPUT: // [object Array] (4)["2","3","4","5"] |
[ABC] | | Characters between brackets: | This type of modifier returns the all-matched character between the bracket. let reg = "Engineering is a good course";let result = reg.match(/[o]/g);console.log(result)OUTPUT:// [object Array] (3)["o","o","o"] |
[A|B] | | Alternatives separated with ‘|’: | This type of modifier returns the alternatives separated with ‘|’. let reg = "mango, ora,orange,apple,appl";let result = reg.match(/(orange|apple)/g);console.log(result)OUTPUT:// [object Array] (2)["orange","apple"] |
METACHARACTERS
Metacharacters are regular expression patterns with a special meaning. This makes the programming easy.
Metacharacter | Description | Example |
\d | To find a digit: This type of metacharacters in the regular expression returns the digits. | let let reg = "He got 90 marks in mathematics";let result = reg.match(/\d/g);console.log(result)OUTPUT:// [object Array] (2)["9","0"] |
\b | To find matching at the beginning of a word:
To find matching at the ending of a word: | let text = "DO YOU PLAY CRICKET";let result = text.search(/\bYO/);console.log(result)OUTPUT:3
let text = "DO YOU PLAY CRICKET";let result = text.search(/PLAY\b/);console.log(result)OUTPUT:7 |
\s | To find whitespace characters: This type of metacharacter in the regular expression returns the whitespace characters. | let reg = "I have driven a bus";let result = reg.match(/\s/g);console.log(result)OUTPUT:// [object Array] (4)[" "," "," "," "] |
The basic regular expression in javascript and their applications are listed above. Try out each feature to learn more about them.