Regular Expression

PHP
In php function for regular expression are
  1. preg_match(regex, stringToSearch); return 0 or 1
  2. preg_match_all(regex, stringToSearch, array_variable)
Example: preg_match(“/^[a-zA-Z ]*$/”, $name)
Read the link first: https://www.w3schools.com/php/php_regex.asp
Javascript
(First, see the image below) JavaScript RegEx link https://www.w3schools.com/jsref/default.asp.

Here is the link of video tutorial: https://clevertechie.com/php/25/php-regular-expressions-tutorial

'/-[0-9]+x[0-9]+\./i' is a string.

/-[0-9]+x[0-9]+\./i is regex.

Example 1:

"hi".match('/hi/')  // returns null
"hi".match(/hi/)    // returns ["hi"]
Example 2:
"Hello Onbiponi, on. Hello".replace(/Hello (.*), (.*)\. Hello/, "$1 $2") //$1=Onbiponi and $2=on
You may use(i.e. /gi at the end) if you need to replace more than one match, but that's it.
Q. Write in a function to show only Bangla, English letters and number. Replace other characters with a hyphen(-).
Answer:
JavaScript:
function sanitize(s) {     s = s.toLowerCase(); s = s.replace(/[^\u0980-\u09FF\u0041-\u005A\u0061-\u007A\u0030-\u0039 ]/ug, ' '); s = s.trim(); return s.replace(/\s+/g, '-'); }
PHP:
function sanitize($slug) { $slug = \strtolower($slug); $slug = preg_replace('/[^\x{0980}-\x{09FF}\x{0041}-x{005A}\x{0061}-\x{007A}\x{0030}-\x{0039} ]/u', ' ', $slug); $slug = trim($slug); return preg_replace('/\s+/', '-', $slug); }

Labels: , ,

© copyright-2020 Rejaul