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:
Rules in regular expressions.
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>
Tips on SEO and Online Business
Next Articles
Previous Articles