Regular expression in JavaScript

What is regular expression?

JavaScript regular expression (regex or regexp) provides a very easy way to match a string with a specific pattern. Regular expression are very powerful tool for searching and finding text. It can match particular characters, numbers, wildcards, and ranges of characters. For example with the use of regex we can do complex form validation or we can find string in whole page.

Basic rules and meanings

  • ^ start with
  • $ end with
  • g Global
  • i Case Insensitive
  • [^r] should not start with r word
  • ^[a] it should be start with a
  • [a-z] it should be between a to z range only one character
  • [0-9] it should be between 0 to 9 range only one character
  • [a-z A-Z] it could be either small a to z or capital A-Z only one character
  • [0-9]{4,5} user can add only 4 or 5 number between 0 to 9.
  • [0-9]+ enter any one digit between 0 to 9 multiple times (plus means least one times and then multiple time)
  • c*a means at least one is not nessessary it will still match if you write only a
  • .abc dot means add any character in keyboard.(&abc)
  • .+ match anything from keyboard.
  • 1234?5 means optional previos character 1235
  • abc|123 means OR a1,b2

Examples

[abc]egular
aegular
begular
cegular

[^r]egular
aegular
zegular

^[0-9]9$
39
59
69

[Regular]ar
Rar
ear

/^[1][0-9]{10,11}$/
18574757588
188858585889

Test regular expression here Go to

Leave a Reply

Your email address will not be published. Required fields are marked *