js create regex from string code example

Example 1: les fonctions reguliere javascript

if(! pseudo.match(/^([a-zA-Z ]+)$/))
    alert('Pseudo invalide');

Example 2: js string to regex

const regex = new RegExp('https:\\/\\/\\w*\\.\\w*.*', 'g');

Example 3: regex for strings with specific letters javascript

let re = /ab+c/;

Example 4: regular expression javascript

// Tests website Regular Expression against document.location (current page url)
if (/^https\:\/\/example\.com\/$/.exec(document.location)){
	console.log("Look mam, I can regex!");
}

Example 5: generate string from regex javascript

console.log(new RandExp(/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$/).gen());
console.log(new RandExp(/^[0-9]{4}$/).gen());
console.log(new RandExp(/^[0-9,A-Z]{4}$/).gen());
console.log(new RandExp(/^([A-Z]){5}([0-9]){4}([A-Z]){1}$/).gen());

Tags:

Java Example