regular expression
A regular expression is a sequence of characters that forms a search pattern.
When you search for data in a text, you can use this search pattern to describe what you are searching for.
A regular expression can be a single character, or a more complicated pattern.
Regular expressions can be used to perform all types of text search and text replace operations.
/pattern/modifiers; /w3Schools/i w3Schools = pattern i = modifier // makes it case- sensitive search
<script>
function myFunction() {
var str = "Visit W3Schools!";
var n = str.search("s"); // returns 2
var n = str.search(/w3Schools/i); // this will return 6
document.getElementById("demo").innerHTML = n;
}
</script>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"W3Schools"); // replaces microsot by"W3Scools"
document.getElementById("demo").innerHTML = txt;
}
</script>
test() and exec() methods for regular expressions.