Regular expression

Regular expression (see also: here… and there…)

Regular expressions play a large role in JavaScript because data systematically move from strings to numbers, JSON… and vice-versa. Regular expressions are shortcuts for checking formats against standards, practices, expectations… A regular expression is expressed between /. Optional flags like i (standing for “case-insensitive”) may be used as ways of performing checking.

class Shopping {
    …
        if (Shopping._Query_content.test(event.key) && event.key.length === 1) { // 'event' from 'keyup'...
    …
}
…
Shopping._GTIN = /^\d+$/; // Numbers only...
Shopping._Query_content = /^[a-z0-9 ]+$/i; // 'Space' included...

Shopping.js.zip 

Regular expression, match

The way regular expressions match strings is instrumented by the match function of the String type (here…). match performs checking against more complicated regular expressions.

CurrenciesController.ISO_code_pattern_as_String_ = "^(?!000)(\\d{3}$)"; // 3 digits, exactly excluding '000'...
CurrenciesController.ISO_symbol_pattern_as_RegExp = new RegExp("^[A-Z]{3}$"); // 3 upper case letters, exactly...
CurrenciesController.Simple_date = RegExp("^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$"); // 'dd/mm/yyyy'...

CurrenciesController.CurrenciesController.ISO_code_pattern_as_RegExp = RegExp(CurrenciesController.ISO_code_pattern_as_String_, "g");
window.alert("111".match(CurrenciesController.CurrenciesController.ISO_code_pattern_as_RegExp)); // '111'
window.alert("000".match(CurrenciesController.CurrenciesController.ISO_code_pattern_as_RegExp)); // 'null'
Google_Images._RH_tag = /"rh":"(amazon\.fr|auchandirect\.fr|bienmanger\.com|carrefour\.fr|catalogue\.cerclevert\.fr|chronodrive\.com|cdiscount\.com|danival\.fr|e-leclerc\.com|episaveurs\.fr|etiketbio\.eu|geantcasino\.fr|intermarche\.com|lagrandeepicerie\.com|lesrobinsdesbio\.com|leaderprice\.fr|lidl\.fr|mangerbiochezmoi\.com|monoprix\.fr|promobutler\.be|provincesbio\.com|prixing\.fr|shoptimise\.fr|webecologie\.com)"/gi;
Google_Images._PT_tag = /"pt":"[^"]*/g;
Google_Images._S_tag = /"s":"[^"]*/g;
// 'g' means all occurrences while 'i' means case-insensitive:
Google_Images._Image_URL_pattern = /"(http(s?):)([/|.|\w|\s|-])*\.(?:gif|jpeg|jpg|png)("|\?)/gi;

Regular expression, beyond Unicode

Regular expressions in JavaScript may overtake Unicode limitations with the u flag especially (here…).

class My_RegExp {
    static Try_it() {
        let my_regex = new RegExp(" ", "g"); // <=> 'const my_regex = / /g;' ('g' flag: all matches)
        window.console.assert(my_regex.global);
        let FB = "Franck        Barbier".replace(my_regex, ""); // window.alert(FB); // 'FranckBarbier'
        my_regex = new RegExp(" ");
        window.console.assert(!my_regex.global);
        FB = "Franck        Barbier".replace(my_regex, ""); // window.alert(FB); // 'Franck       Barbier'
        my_regex = /[\ud800-\udbff][\udc00-\udfff]/; // https://thekevinscott.com/emojis-in-javascript/
        const smiley = String.fromCodePoint(128516);
        window.alert(smiley + "? " + my_regex.test(smiley)); // '😄? true'
        const any_currency = /\p{Sc}/gu; // '\p{Sc}' means: any currency symbol ('/u' not yet supported by Firefox?)
        const prices = `Prices: $2, €1, ¥9`; // window.alert(prices.match(any_currency)); // '$,€,¥'
    }
}
My_RegExp.Try_it();

My_RegExp.js.zip 

© Franck Barbier