Regular Expression

Regular Expression is used to search and manipulate text based on patterns in javascript. Here are the basic that we need to know in order to proceed further.

Regular Expression Basic

A normal regular expression will look like this

/abc/gi

The above code will match "vsadfABCasd", "abcScw" or any combination that contain 'abc' regardless of case sensitive due to 'gi' given at the end of the pattern where the meaning are describe below,

ModifierDescription
gDo global pattern matching.
iDo case-insensitive pattern matching.

Some characters don't match themselves, but are metacharacters. You can match these characters literally by placing a backslash in front of them. For example, "" matches a backslash and "$" matches a dollar-sign. Here's the list of metacharacters:

 | () [ { ^ $ * + ? .

A backslash also turns an alphanumeric character into a metacharacter. So whenever you see a backslash followed by an alphanumeric character:

d D w W t s 3

you can get the meaning of the above metacharacter and a list of javascript regular express methods from here.

/Homer|Marge|Bart|Lisa|Maggie/

Any of those strings can trigger a match. That is, the preceding expression matches all of the following strings:

  • "Homer"
  • "Bart"
  • "Lisa Simpson"
  • "Simpson, Marge"

You can group various sorts with parentheses, as in the following expression: