node.js regexp everything code example

Example 1: regular expression flags

Besides the regular expressions, flags can also be used to help developers with pattern matching.
/* matching a specific string */
regex = /sing/; // looks for the string between the forward slashes 9case-sensitive)… matches “sing”, “sing123”
regex = /sing/i; // looks for the string between the forward slashes (case-insensitive)... matches "sing", "SinNG", "123SinNG"
regex = /hello/g; // looks for multiple occurrences of string between the forward slashes...
/* groups */
regex = /it is (sizzling )?hot outside/; // matches "it is sizzling hot outside" and "it is hot outside"
regex = /it is (?:sizzling )?hot outside/; // same as above except it is a non-capturing group
regex = /do (dogs) like pizza 1/; // matches "do dogs like pizza dogs"
regex = /do (dogs) like (pizza)? do 2 1 like you?/; // matches "do dogs like pizza? do pizza dogs like you?"
/* look-ahead and look-behind */
regex = /d(?=r)/; // matches 'd' only if it is followed by 'r', but 'r' will not be part of the overall regex match
regex = / (?<=r)d /; // matches 'd' only if it is proceeded by an 'r', but 'r' will not be part of the overall regex match

Example 2: js string to regex

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

Example 3: using regex in javascript

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

Example 4: what is regular expression in javascript

let re = /ol/, 
Characters \, ., \cX, \d, \D, \f, \n, \r, \s, \S, \t, \v, \w, \W, \0, \xhh, \uhhhh, \uhhhhh, [\b]	
Assertions 	^, $, x(?=y), x(?!y), (?<=y)x, (?<!y)x, \b, \B
Groups 		(x), (?:x), (?<Name>x), x|y, [xyz], [^xyz], \Number	
Quantifiers *, +, ?, x{n}, x{n,}, x{n,m}
Unicode \p{UnicodeProperty}, \P{UnicodeProperty}	 property escapes
let defaults = new RegExp('compiled'); 
defaults = { dotAll: false, flags: "", global: false, ignoreCase: false, falselastIndex: 0, multiline: false, source: "abc", sticky: false, unicode: false}