Regular Expression

Please I need explanation on the use of underscore (_), lookaheads,$1,and $2 in regex patterns

Can you be more specific about what you want help with?

I need explanation on the use of “_”, $1,$2,and lookaheads in regular expressions


$1-$9 are mainly used in the string replace() function as a placeholder to store the matched result.

var a = ‘Hello world’;
var regEx = /(\w+)\s(\w+)/;
a.replace(regEx, ‘$2, $1’);
$1 holds the first match and $2 holds the second match
RegExp.$1 will give you ‘Hello’ and RegExp.$2 will give you ‘world’

lookaheads are used to match a pattern that is followed by another pattern
Syntax for lookahead is : X(?=Y) , it means "look for ‘X’ , but match only if followed by ‘Y’

for more on regex you can refre Javascript MDN link:

Thanks, I really appreciate