Unikaksha: Regular expressions:
a regular expression often refered to as regex, is a sequence of characters that
forms a searcg pattern. It is a powerful tool for pattern matching and manipulating
of strings in javascript and many other programming languages.
create a regex using two methods:
1) regular expression literal:
involves enclosing the regex pattern between two slashes(/) and using it directly
in the code.
let regex = /pattern/;
2) regular expression constructor:
let regex = new RegExp("pattern");
literal characters:
literal characters are used to match exact characters or sequence of characters.
1) exact matching:
/abc/ matches the exact sequence of characters "abc".
2) case insensitive matching:
/abc/i - matches the sequence "abc" while ignoring the case.
3) characters matching:
i) /[aeiou]/ - matches any single character in the set "a","e","i","o","u".
ii) /[0-9]/ - matches any digits characters.
iii) /[a-z]/ - matches any lowercase letter.
iv) /[A-Z]/ - matches any uppercase letter.
v) /[^0-9]/ - matches any charcter that is not a digit.
4) escape sequence:
i) /\// - matches a forward slash character ("/").
ii) /\$/ - matches a dollar sign character.
iii) /\\n/ - matches a newline character("\n").
5) unicode characters:
/u{}/ sytax - to specifying the unicode character in regex.
/u{1F600}/ - representing the grinning face emoji.
/u - flag at the end of the regex indicates that it supports unicode characters.
/u{1F600}/u - matches a specific unicode character using its code point.
1)exact matching:
let regex = /abc/;
console.log(regex.test("abc"));
console.log(regex.test("abcd"));
console.log(regex.test("dadbdc"));
console.log(regex.test("ab"));
2) case insentivity:
let regex = /abc/i;
console.log(regex.test("abc"));
console.log(regex.test("Abc"));
console.log(regex.test("ABC"));
console.log(regex.test("aBC"));
console.log(regex.test("aBc"));
console.log(regex.test("aBdC"));
console.log(regex.test("aaBbC"));
3) character matching:
i)
let regex = /[aeiou]/;
console.log(regex.test("apple"));
console.log(regex.test("banana"));
console.log(regex.test("zzzzz"));
ii)
let regex = /[0-9]/;
console.log(regex.test("abc123"));
console.log(regex.test("123"));
console.log(regex.test("a1b2c3d4"));
console.log(regex.test("abc"));
iii)
let regex = /[a-z]/;
console.log(regex.test("abc123"));
console.log(regex.test("123"));
console.log(regex.test("a1b2c3d4"));
console.log(regex.test("ABC"));
iv)
let regex1 = /[A-Z]/;
console.log(regex1.test("abc123"));
console.log(regex1.test("123"));
console.log(regex1.test("a1b2c3d4"));
console.log(regex1.test("abC"));
v)
let regex = /[^0-9]/;
console.log(regex.test("abc123"));
console.log(regex.test("123"));
console.log(regex.test("a1b2c3d4"));
console.log(regex.test("abC"));
console.log(regex.test("!@#$%^&*"));
4) escape charcters:
i)
let regex = /\//;
console.log(regex.test("http://example.com/"));
console.log(regex.test("http:example.com"));
ii)
let regex = /\$/;
console.log(regex.test("the price is $10"));
console.log(regex.test("the price is 10 dollar"));
iii)
let regex = /\\n/;
console.log(regex.test("New \\n line"));
console.log(regex.test("New Line"));
5) unicode characters:
let regex = /\u{1F600}/u;
console.log(regex.test("hello 😀"));
console.log(regex.test("hello ðŸ˜"));
Meta characters:
0 Comments