From 6518b7d5fa018f398483fa34ffbe1d647478b286 Mon Sep 17 00:00:00 2001 From: Lars Windolf Date: Wed, 8 Nov 2023 21:36:19 +0100 Subject: [PATCH] Update Javascript RegExp.md --- examples/Javascript Examples/Javascript RegExp.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/Javascript Examples/Javascript RegExp.md b/examples/Javascript Examples/Javascript RegExp.md index b0059eb69..0c4a178b0 100644 --- a/examples/Javascript Examples/Javascript RegExp.md +++ b/examples/Javascript Examples/Javascript RegExp.md @@ -19,6 +19,14 @@ string for the entire match and one for each capture group. If a pattern doesn't match null is returned. +## Named Capture Groups + +For more complex expressions name captures for better code: + + var results = "some.person@example.com".match(/(?.+)@(?.+)/); + console.log(results.groups[name]); # Gives "some.person" + console.log(results.groups[domain]); # Gives "example.com" + ## Replacing Patterns To replace substrings by pattern just pass a regular expression to String.replace() @@ -41,3 +49,7 @@ To reuse expressions assign them to a variable or explicitely create an RegExp o var re = /abc/; var re = new RegExp("abc"); + +and use it + + "abcdef".match(re);