PKB
PKB

Reputation: 81

Javascript regex: how to not capture an optional string on the right side

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:

  1. The above string is just for example - we dont want to assume anything about the suffix string (.com above). It could as well be orange.
  2. Just this part can be solved in C# by matching from right to left (I dont know of a way of doing that in JS though) but that will end up having www. included then!
  3. Sure, this problem as such is easily solvable mixing regex with other string methods like replace / substring. But is there a solution with only regex?

Upvotes: 2

Views: 436

Answers (3)

vks
vks

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

mMontu
mMontu

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

Hayden Schiff
Hayden Schiff

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

Related Questions