Javascript - Regular expression (Regexp)

About

Multilingual Regular Expression Syntax (Pattern) in Javascript

Regular expressions operate at the level of code units. The single-character pattern (.) matches a single code unit (not a Text - Code point).

console.log(/^.$/.test("𐌰"));
console.log(/^..$/.test("𐌰"));

Syntax

  • The pattern enclosed between slashes. Loaded compilation (only when the script is loaded)
var pattern = /ni.o/; 
  • Runtime compilation (when the script runs, the regexp pattern can be dynamic)
var pattern = new RegExp("ni.o", flags); 

Example

Runtime

var pattern = new RegExp("f.o","i"); 
console.log(pattern.test("foo"));
console.log(pattern.test("bar"));
console.log(pattern.test("FOO"));

Named Group

Named Group proposition

let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
let result = re.exec('2015-01-02');
console.log(result.groups.year); // '2015'
console.log(result.groups.month); // '01'
console.log(result.groups.day); // '02';

console.log(result[0]); // '2015-01-02';
console.log(result[1]); // '2015';
console.log(result[2]); // '01';
console.log(result[3]); // '02';

Named Group Replacement

let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
let result = '2015-01-02'.replace(re, '$<day>/$<month>/$<year>');
console.log(result);// '02/01/2015'
  • Routing
re = new RegExp("/api/profiles/(?<id>[^/]*)");
let result = '/api/profiles/1'.replace(re, 'backend/api/profiles/profile.js?id=$<id>');
console.log(result);

Loaded

const REGEXP_EXP = /^PREFIZ_/i;
const raw = Object.keys(process.env)
    .filter(key => REGEXP_EXP.test(key))
    .reduce(
      (env, key) => {
        env[key] = process.env[key];
        return env;
      }
    );

Library

Documentation / Reference





Discover More
D3 Csv Console Log
D3 - Csv Function (CSV from an URL )

This section talks the function d3.csv that parse a CSV file. To parse a CSV string, see . Reference: Csv The csv file or string must...
Color Autocompletion List
How to create an Autocompletion functionality with CSS, Javascript and HTML

This article shows you how to create a search box with an autocompletion feature
Javascript - Special Characters

Characters that can act as: Expression operator (ie an operator) The beginning of a statement ( [ + Concat or addition - Minus / Division Regular expression Example: / as a regular...
Javascript - String

The in javascript. A string in JavaScript is encoded with the ucs-2 16-bit character set. An element of a JavaScript string is therefore a 16-bit code unit. code unitscode pointssurrogate pair Strings...
Javascript - Type

Type in Javascript Javascript is not static typed but it exist Javascript wrapper language that implements it. See Only values have types in JavaScript; variables are just simple containers for those...
Regexp
Multilingual Regular Expression Syntax (Pattern)

Regular expression are Expression that defines a pattern in text. This is therefore a language that permits to define structure of a text. They are a mathematically-defined concept, invented by Stephen...
Robots Useragent
Web - Robots (Wanderers | Crawlers | Spiders)

This page is in a web context. Web Robots (also known as Web Wanderers, Crawlers, or Spiders), are crawler program that scan the web generally in order to: create an search engine. See or seo...



Share this page:
Follow us:
Task Runner