Javascript-RegExp
In JavaScript, a Regular Expression is an object that describes a sequence of characters used for defining a search pattern.
For example:
/^a...s$/
The pattern is: any five letters string starting with a and ending with s.
Two ways to create a RegExp object:
- literal notation – slashes and do not use quotation marks.
Example: cost regularExp = /abc/;
- constructor – slashes but do use quotation marks.
Example: const reguarExp = new RegExp(‘abc’);
Syntax:
/pattern/modifiers;
Static properties:
- get RegExp[@@species] – used to create derived objects.
- lastIndex – index at which to start the next match.
Regular Expression Modifiers:
case-insensitive more global searches
Modifier Description
i Perform case-insensitive matching.
g Perform a global match .
m Perform multiline matching.
Regular Expression Patterns:
Expression Description
[abc] Any characters between the brackets
[0-9] Any digits between the brackets
(x|y) alternatives separated with |
Using String Methods:
Used with the two string methods:
1.search()
2.replace()
search() With a String:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>Search a string for "I2tutorials", and display the position of the match:</p>
<p id="demo"></p>
<script>
var str = "Visit I2tutorials!";
var n = str.search("I2tutorials");
document.getElementById("demo").innerHTML = n;
</script>
</body>
</html>
OUTPUT:
JavaScript String Methods
Search a string for “I2tutorials”, and display the position of the match:
7
search() With a Regular Expression:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p>Search a string for "I2tutorials", and display the position of the match:</p>
<p id="demo"></p>
<script>
var str = "Visit I2tutorials!";
var n = str.search(/I2tutorials/i);
document.getElementById("demo").innerHTML = n;
</script>
</body>
</html>
OUTPUT:
JavaScript Regular Expressions
Search a string for “I2tutorials”, and display the position of the match:
7
replace() With a String:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript String Methods</h2>
<p>Replace "Microsoft" with "I2tutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="replace">Please visit Microsoft!</p>
<script>
function myFunction() {
var str = document.getElementById("replace").innerHTML;
var txt = str.replace("Microsoft","I2tutorials");
document.getElementById("replace").innerHTML = txt;
}
</script>
</body>
</html>
OUTPUT:
JavaScript String Methods
Replace “Microsoft” with “I2tutorials” in the paragraph below:
Please visit I2tutorials!
replace() With a Regular Expression:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p>Replace "microsoft" with "I2tutorials" in the paragraph below:</p>
<button onclick="myFunction()">Try it</button>
<p id="replace">Please visit Microsoft and Microsoft!</p>
<script>
function myFunction() {
var str = document.getElementById("replace").innerHTML;
var txt = str.replace(/microsoft/i,"I2tutorials");
document.getElementById("replace").innerHTML = txt;
}
</script>
</body>
</html>
OUTPUT:
JavaScript Regular Expressions
Replace “microsoft” with “I2tutorials” in the paragraph below:
Please visit I2tutorials and Microsoft!