regexp object javascript code example

Example 1: javascript new Regexp with flag

new RegExp(/ab+c/, 'i') // literal notation
new RegExp('ab+c', 'i') // constructor

const pattern='some pattern';
new RegExp(pattern)

Example 2: using regex in javascript

//Adding '/' around regex
var regex = /\s/g;
//or using RegExp
var regex = new RegExp("\s", "g");

Example 3: 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!");
}

Tags:

Dart Example