regex to match exact string 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: regex match exact string
you want to achieve a case insensitive match for the word "rocket"
surrounded by non-alphanumeric characters. A regex that would work would be:
\W*((?i)rocket(?-i))\W*
Example 3: javascript regex exact match
var r = /^a$/
function matchExact(r, str) {
var match = str.match(r);
return match && str === match[0];
}