Reputation: 81
For example /(www\.)?(.+)(\.com)?/.exec("www.something.com")
will result with 'something.com' at index 1 of the resulting array. But what if we want to capture only 'something' in a capturing group?
Clarifications:
Upvotes: 2
Views: 436
Reputation: 67968
(?:www\.)?(.+?)(?:\.com|$)
This will give only something
ingroups
.Just make other groups non capturing.See demo.
https://regex101.com/r/rO0yD8/4
Upvotes: 2
Reputation: 9273
Just removing the last character (?
) from the regex does the trick:
https://regex101.com/r/uR0iD2/1
The last ?
allows a valid output without the (\.com)
matching anything, so the (.+)
can match all the characters after the www.
.
Another option is to replace the greedy quantifier +
, which always tries to match as much characters as possible, with the +?
, which tries to match as less characters as possible:
(www\.)?(.+?)(\.com)?$
https://regex101.com/r/oY7fE0/2
Note that it is necessary to force a match with the entire string through the end of line anchor ($
).
Upvotes: 0
Reputation: 3330
If you only want to capture "something", use non-capturing groups for the other sections:
/(?:www\.)?(.+)(?:\.com)?/.exec("www.something.com")
The ?:
denotes the groups as non-capturing.
Upvotes: 0