match regex javascript code example
Example 1: javascript regex example match
let reg = /abc/
reg = new RegExp('abc')
let str = 'Abc abc abc'
str.match(/abc/)
str.match(/abc/g)
str.match(/abc/i)
str.match(/abc/ig)
str.match('abc', 'ig')
Example 2: js string to regex
const regex = new RegExp('https:\\/\\/\\w*\\.\\w*.*', 'g');
Example 3: js match any number string
const match = 'some/path/123'.match(/\/(\d+)/)
const id = match[1]
Example 4: Define REGEXP?
REGEXP is a pattern match in which matches pattern anywhere in the search value.
Example 5: how to use the match function in javascript for regex
const str = 'For more information, see Chapter 3.4.5.1';
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);
console.log(found);
Example 6: javascript regex exact match
var r = /^a$/
function matchExact(r, str) {
var match = str.match(r);
return match && str === match[0];
}