30
loading...
This website collects cookies to deliver better user experience
Regular Expressions
or we can also call it REGEX
.REGEX
is sequence of characters which are in a certain patter, and these patterns help us find
or find and replace
or validate
things like email, passwords and usernames
. regex
with an example is if:JavaScript
in a string.REGEX
has lots and lots of use-cases.dog
or cat
.|
the OR
sign.petString
would contain Shreyas loves JavaScript
then the output would have been false
.shreyas
and I search for ShreyaS
then the output would be false
.i
in regex1
, and there are many such flags which gives us a lot of control over the pattern.i
stands for irrespective of the case
..test()
method, which is an inbuilt method in JavaScript, which returns true or false according to the pattern entered.pattern.test(String-which-has-to-be-tested)
. .test()
has a draw back, which is that it only returns true or false, and if true it does not tell us how many times the pattern was matched, so to back this drawback, JS has another inbuilt method called as .match()
which let's us know how many times the pattern is matched in the string..match()
return an array of results which have successfully matched the pattern, and the array's length is the time the pattern was recognized..match()
is a little bit different when compared to .test()
.match()
's syntax is : string.match(regex-pattern);
g
and it stands for global
, which helps us find the perfect match globally in the string.Character Classes
, these allow us to define a group of characters and they have to be enclosed in [ ]
(Square Brackets) .[aeiou]
vowels are grouped together and are individually searched in the string..
period/dot. un
. For example fun
or run
or sun
.i
flag) and would search in the whole string (g
flag).at
, then we can give a range of characters which will check the string and if matched then return an array./[0-9]/g
, that's it, all the numbers are covered./[0-9]/
when you can also write /[\d]/g
and this d
stands for digits!/\w/g
and instead of the whole REGEX pattern you can just write the shorthand./[regex here]{min-number, max-number}/g
.