![]() ![]() function ( match, g1, g2, g3, offset, string, groups ) ) Instead of passing a replacement string, we can use a more powerful function instead. It’s times like this when you need conditional logic or slightly more complex replacements, that the replacer function is needed. log (result ) // Mike Johnson // … // Mark Sam Bridge Note that I've also added a question mark before the new middleName group, and that's because only people with a middle name have a space after their first name. * To create an option group you can add a ? after the group. Now names without a middle name will have an extra space between the first and last name. Now you create an optional* third group to collect those middle names, and that would work, but in our replacement strings it would mess up the spacing. And it’s messing up your code!Ĭurrently, those middle names are being missed, and your new address book is incorrect. Let’s say in your address book string, some people have middle names. ![]() This gives you more power with your replacement and your groups. Instead of passing a new string directly to replace() like we just did, you can also give it a function. Going back to our name replacement, let’s name the groups. In a replace, you can insert the group by its name like this $. ![]() Instead of (\w ), you can add a name with ? at the start of the group, like (? \w ). To name a group, you give it a name when you create it. (Don’t actually forget $1 and $2, it’s still the quickest way to create and access groups if you just have a few!) If you start to struggle to know what order they are in, there’s a better way! Naming regex groupsįorget $1 and $2, you can name your groups something more memorable instead. The replacement "$2 $1" simply means, return the second group, a space, then the first group.Īccessing groups this way is great until you have a lot of them. Then for the groups, we wrap the two name parts of the match in parenthesis /(\w ), (\w )/gm so that we can access those parts of the match in the replacement. The gm flags make the match g global (multiple matches) since we want to match many names and m multi-line since our string is across multiple lines. The regex here is /\w , \w /gm with \w matching any word character and matching between one and unlimited times (greedy). replace(), because $1, $2 etc tokens are easy to remember and you can transform long strings quickly. In seconds!Īccessing the group in a replace is similar to what we did in the last blog post match, but instead of result you can use $1 (or if it's the second group $2).Īnd this also works with the g global flag without having to loop over an object or an array (like we had to when performing a match). But if you know regex (and regex groups) you can automate the transformation. It would take a long time to go through each name and edit it manually. But you need the names presented differently. The list has over 1000 names, and it looks like this: Johnson, MikeĮach name is on a new line, and the pattern is. In this first example, you have a list of names, maybe from an address book or some other service. Let’s look at a real example to show how useful regex groups can be. replace() function can also use regex, you will create groups in the same way.īut how you can use those groups with. In the last blog post, we looked at ( regex groups). ![]() One of the places I find regex groups most useful is when using regular expressions for replacement. In this blog post, discover how to use multiple groups to perform simple (and complex) replacements. Regex groups are super useful when you need to replace specific patterns in strings. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |