1

I am trying to grab a digit (5) followed by a possible - or (space) proceeding a sequence of 3 digits, followed finally by a single digit. If the first group doesn't match at all, then only return the other sequences of digits.

^(5\-? ?)?(\d{3})(\d)$

This looks right to me and doesn't throw any errors, but it's giving the 5 back:

"5489" -> ()("548")("9")

Where I would actually not want this expression to return a match for this pattern.

So a quick search brought me to possessive expressions and a lot of articles about your ex. From what I'm reading, this looks like it should work:

^(5\-? ?)?+(\d{3})(\d)$

But Javascript does not like that as a regular expression.

Is there a way to do a greedy possessive capture group in Javascript, or simulate it in this situation?

7
  • 1
    No possessive quant's in JS ?+ Commented Jun 26, 2017 at 19:48
  • 1
    If you have a look at the ECMAScript 5 docs, you will see there is no support for possessive quantifiers. Commented Jun 26, 2017 at 19:51
  • 1
    If it's just not 5 then this ^(5\-? ?)?((?!5)\d{3})(\d)$ Commented Jun 26, 2017 at 19:52
  • 1
    Try ^(?!5\d{3}$)(5\-? ?)?(\d{3})(\d)$. It will fail a match at once if the string starts with 5 and then has 3 digits. Commented Jun 26, 2017 at 19:55
  • 1
    @RandyHall - There is no reason to force anything. You've got a fixed amount of digits to match at the end, plus you're using anchors ^$. The conclusion is that if you have 4 digits only they will be matched at the end because its a tenent of the regex design. If you don't want to match 4 digits starting with a 5, then it's an extra assertion, but has nothing to do with forcing a capture of the first group. I'm just stating this as a comment because it's an X/Y problem, and I don't like to post for no reason. Commented Jun 26, 2017 at 20:05

2 Answers 2

3

You can simulate possessive quantifier functionality by taking advantage of lookaround qualities:

^(?=(...))\1

Regex:

^(?=((5\-? ?)?))\1(\d{3})(\d)$

Live demo

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I think this is the better answer. One, that it simulates possessive, two, that it won't match 5890 but will match 55890.
1

If you have a look at the ECMAScript 5 docs, you will see there is no support for possessive quantifiers.

You may use

^(?!5\d{3}$)(5-? ?)?(\d{3})(\d)$

See the regex demo. The (?!5\d{3}$) negative lookahead will fail a match at once if the string starts with 5 and then has 3 digits.

Details:

  • ^ - start of string
  • (?!5\d{3}$) - there cannot be 5 and then 3 digits and the end of string immediately to the right of the current location
  • (5-? ?)? - an optional sequence of 5, then an optional - and then an optional space
  • (\d{3}) - 3 digits
  • (\d) - one digit
  • $ - end of string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.