Javascript Regular Expression Operation

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-regular-expression-operation/
PINTEREST
PINTEREST
INSTAGRAM

Regular expression is the language to define specific pattern for text data. Javascript supports regular expression. Regular expression is generally used like below. We will look different type of usage examples of regular expression.

  • str is the text data we want to regex.
  • re is our regex object to regex.
  • re.exec() gets text data and executes regular expression. This will return an array of matched strings which is [“poftut”] in this example

Regex Only Alphabet Characters

We can set a match for alphabet characters like below.

  • re.exec() will return first occurence of the matching character which is T

Not Match Given Characters

We can provide negative match by providing characters.

  • [^a-Z] means do not match those characters. As expected result will be space character.

Match Any Decimal Digit

We can match decimal digites like below

LEARN MORE  Awk Regular Expression Commands and Examples

  • [0-9] means characters between 0 and 9 which are decimal digits. Results will be 9.

Match Any Lowercase Character

Match Any Uppercase Characters

Quantifiers

We can set the frequency of regex expression with different operators and we call them quantifiers.

One or More

If we want to look regex expression which occure one or more time we can use + .

  • We match (a)+ where one or more a exists. Result is an array like [“a”,”a”]

Zero or More

Similar to one or more but zero occurence is accepted too. We can use asterisk for this operation.

Match Only One Character

We can look only one character like a? .

Match Specified Times

We can set minimum and maximum occurence with {} .

  • Match minum 2 maximum 2 occurence of which is aa . result will be [ “aa” ]
LEARN MORE  Multiline Strings in Python

Match End of Line

We can match by using end of line

  • 9$ means line ends with 9. We get [“9”] as result.

Match Start of Line

We can match by using start of line

  • ^This means line starts with This. We get [“This”] as result.
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-regular-expression-operation/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)