Non-capturing groups are excluded from the result. Use the (?:) syntax to match non-capturing groups in your strings. log (result ) // 'Isabell' const notMatchingResult = regex. It turns out that you can define non-capturing groups that are not included in the result! // a regular expression with two non-capturing groups // and one capturing group const regex = / (?:Jane|John|Alison)\s(.*?)\s(?:Smith|Smuth) / const result = regex. There's nothing particularly wrong with this approach, but to extract the desired middle name, you have to remember and go back to the original regular expression because the result includes several irrelevant values ( 1 and 3). Even though you're only interested in the middle name represented by the entry at index 2 you have to deal with the data for all groups. In this example, the array holds the character sequences matching the three defined capturing groups. The array includes the full matching string at index 0 followed by the defined groups ( 1, 2, etc.). The capture group in the following regex works in CEP but not. If the string matches the expression, the return value is an array holding all the specific information, otherwise exec returns null. I am doing a migration from Sybase CEP to ESP and I have a problem with regexpfirstsearch(). Run the regular expression's exec method to test if a string is matching the expression. log (result ) // 'Jane Isabell Smith'Ĭonsole. The name should begin with Jane, John or Alison, end with Smith or Smuth but include a middle name between the first and last name. The regular expression above defines that I'm looking for a very particular name combination. a regular expression including three capture groups const regex = / (Jane|John|Alison)\s(.*?)\s(Smith|Smuth) / To understand how non-capturing groups work, let's look at an example highlighting the well-known capturing groups. If you already know what non-capturing groups in regular expressions are, here's the syntax: it's (?:) as in /(?:non-caputuring group)/.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |