JavaScript | Regular expressions Advanced
To implement regular expressions in JavaScript:
str.replace(/[0-9]/gi,"a");
If the str contains ab09, then the above function will return
str=abaa
Regular expressions are used in pattern matching, i.e., to find if a particular pattren exists in a string.
It takes optional parameters 'g' and 'i':-
Every regular expression has two properties:-
- g - It specifies that all the matching characters should be replaced. If g is not specified only the first matching character will be replaced
- i - The i stands for ignore case
i.e., if the regular experession is /[a-c]/i,"d"
then the str="aAbBcC" will be transforemed to Str="dddddd Both the lowercase and uppercase characters will be transformed. If i is not specified, only the lowercase charcaters will be transformed. for example, if regular expression is /[a-c]/, then the string will become "dAdBdC".
[0-9]:- matches all single numeric charcters
[abc]:- matches one of "a","b","c" characters
[abc]:- matches one of "a","b","c" characters
trim() function:
To remove the unwanted regular expressions from the left and right ends of a string, we use
str.trim()
for example,
if
str=" gaurav ";
then
str.trim()
will return str="gaurav";
Comments
Post a Comment