Regular Expressions in Javascript


Published on 13 April 2022


Regular Expressions in Javascript

Explaining the meaning of regular expressions or patterns in javascript

A regular expression is an object that through a text expression tries to find and replace information within text strings.
A regular expression has the following basic structure:

/regular-expression/[parameters]

Example:
var mystring = "mytext";
var regExp = /searching/g;

An example is the following, we define the regular expression within the two / and between parentheses we will place an expression "hhh", that is, the match hhh will be searched for within a string. In parameters we will define the way in which it is searched, that is, "g", its meaning is global, which allows searching in the entire text string, the "i" allows not to make differences between upper and lower case letters within the searched text:

var regExp = /hhh/gi;
var mystring = (hhhello, HHHello, HhHello);

Methods used in regular expressions:

  • search. Lets you know if a pattern matches in a string. If positive, it returns the number of matches or the position of the pattern within the string, otherwise it returns "-1".
  • exec. Find matches in the expression by returning an array with all matches.
  • test. Finds if a pattern is present in a string, returning true or false.
  • match. Returns all matches in a string.

Rules in regular expressions.

  • Spot ".". It is interpreted in the regular expression as any character.
  • Backslash "\". Used for the next character to have a special meaning. Example: When using "\.", the dot will be interpreted literally.
  • Special characters.
    \t: represents a tab
    \r: represents a return
    \n: represents a line break
    \d: represents a digit from 0 to 9
    \w: represents any alphanumeric character including the underscore "_"
    \s: represents a blank space
  • Brackets "[]".
    Create group of characters
    Use a hyphen to indicate ranges
  • Slash "|". Indicate different options
  • Dollar "$".
    Represents the end of the string
    It can represent the end of a line or position.
    Example:
    ".$": Represents where the point ends a line
  • Symbol ^. Indicates the match of one or more characters at the beginning of a string.
    Example:
    "^my": my must be at the beginning of the string
  • Symbol ?. The character or group of characters before the question mark is optional and can appear at most once.
    Example: "m?": m is optional within a string
  • Symbol *. The number of the character in the group before the asterisk can be random (including zero).

Example:
We will check in the following "javascript script" the use of regular expressions, looking for matches within a string called "mystring".

 <script type="text/javascript">
 var mystring = "this is my first text / another text";
 
 //We are looking for a slash "\"
 var regExp = /\//g;
 console.log('We are looking for a slash "\": ' + mystring.search(regExp));

 //We are looking for an expression with lowercase letters from "a" to "z", ending at that moment "$"
 var regExp = /[a-z]$/g;

 console.log('We are looking for an expression with lowercase letters from "a" to "z", ending at that moment "$"');
 console.log(mystring.search(regExp));
 console.log(regExp.test(mystring));

 //The dot in regExp indicates that the last character is whatever
 var mystring = "thisSS";
 var regExp2 = /thi./g;
 console.log(mystring.search(regExp2));
 console.log(regExp2.test(mystring));

 //The dot in regExp indicates that the last character is a dot
 var mystring = "this.";
 var regExp2 = /this\./g;
 console.log(mystring.search(regExp2));
 console.log(regExp2.test(mystring));

 //The "+" in regExp indicates if a pattern is true, if a word ends in yes or not
 var mystring = "this";
 var regExp2 = /s+$/g;
 console.log(mystring.search(regExp2));
 console.log(regExp2.test(mystring));

</script> 

Example of validating a URL and an Email with a regular expression:

 <script type="text/javascript">
 var url = "https://www.lookkle.com";
 var regExp = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i;
 console.log(regExp.test(url));

 var email = "myemail@hello.com";
 var regExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
 console.log(regExp.test(email));
</script> 


Previous Chapter - Next Chapter



Tips on SEO and Online Business

Next Articles

Previous Articles