JSFiddle - React, Tailwind, and code Playground
HTML
var patt = /w3schools/i <br>
i for case in case-insensitive search
JavaScript
var alert = function (str) {
var st = document.createTextNode(str);
var p = document.createElement('p');
p.appendChild(st);
document.querySelector('body').appendChild(p);
}
//regular expression in javascript
/*
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.
*/
var patt = /w3schools/i
/*
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-insensitive).
*/
var str = "Visit W3Schools";
var n = str.search(/w3schools/i); // return postion if found
alert(n)
// we can also do
var n = str.search("W3Schools");
alert(n)
var str = "Visit Microsoft!";
// this replace function , replace the String and return result string
var res = str.replace(/microsoft/i, "W3Schools");
alert(res)
var str = "Visit W3Schools";
var pattern = /w3schools/i;
var resu = pattern.test(str);
alert(1)
alert(resu)
alert(2)
alert(/abc/.test('abcde'))
alert(/abc/.test('abdec'))
alert(3)
// validation for only 5 digits
alert(/^\d{5}$/.test('12345'))
alert(/^\d{5}$/.test('123456'))
alert(/^\d{5}$/.test('1234F'))
/* ^ indicates the beginning of the string. Using a ^ metacharacter requires that the match start at the beginning.
\d indicates a digit character and the {5} following it means that there must be 5 consecutive digit characters.
$ indicates the end of the string. Using a $ metacharacter requires that the match end at the end of the string.
*/
alert(4)
// digits only
alert(/\d/.test('11111'))
alert(/\d/.test('FFFF'))
alert(5)
// digit not allowed
alert(/[^0-9]/.test('111')) // false
alert(/[^0-9]/.test('asdasd')) // true
alert(6)
// characters only
alert(/^[a-zA-Z]+$/.test('abcdes'))
alert(/^[a-zA-Z]+$/.test('abcdes123'))
alert(7)
/* Matches end of input.
For example, /t$/ does not match the 't' in "eater", but does match...