Skip to content

Add beginCapture and endCapture #17

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jul 25, 2013
Merged

Add beginCapture and endCapture #17

merged 1 commit into from
Jul 25, 2013

Conversation

go-oleg
Copy link
Member

@go-oleg go-oleg commented Jul 23, 2013

Hi,

First off, this tool is a great idea!!!

About this PR:

I added beginCapture and endCapture functions to support capturing groups. Here is an example:

var tester = VerEx()
            .startOfLine().beginCapture()
            .then( "http" )
            .maybe( "s" ).endCapture()
            .then( "://" )
            .maybe( "www." ).beginCapture()
            .anythingBut( " " ).endCapture()
            .endOfLine();

var matches = tester.exec("https://www.google.com")
//matches: ["https://www.google.com", "https", "google.com"]

To achieve this, I also changed all other ( used in the code to non-capturing ones (?: so that the regex wouldn't capture anything the user did not expect.

Please be sure to review my changes to the or function since I removed the extra check on this._prefixes and this._suffixes because I couldn't quite figure out what usecase those were addressing.

Overall, this pattern is a slight divergence from what you have going right now in that it takes two function calls to achieve something. The other option would be to make it more like or() behaves and have a capture() function that just captures everything before it. However, I think beginCapture()/endCapture() is a bit more flexible and allows you to have multiple separate capturing groups which is probably pretty common.

Please let me know what you think. I'd be happy to add info to README.md and the wiki if you plan on merging it.

Thanks,
Oleg

PS - If you like these functions, I was thinking we could even take it a step further and allow users to pass in a name into beginCapture and then provide a more user-friendly way of accessing the captured values than the array returned by exec().

@ryan-endacott
Copy link
Member

👍

go-oleg added a commit that referenced this pull request Jul 25, 2013
Add beginCapture and endCapture
@go-oleg go-oleg merged commit cdc9c21 into VerbalExpressions:master Jul 25, 2013
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants