0

How can i do the following:

I have a string

something#12

I need a regex that will get last two numbers from the string in range from 0 to 19. Note that nothing should be returned if the number after # is greater than 19, i need to ignore letters and special symbols either.

I have tried the following:

([0-9]{1}$)|([0-1]{1}[0-9]{1}$)

but it return last one or two numbers if i have a value greater than 19.

4 Answers 4

2

Just add the # before the digit:

#([0-9]{1}$)|([0-1]{1}[0-9]{1}$)

or simpler:

#[01]?\d$
Sign up to request clarification or add additional context in comments.

Comments

1

I think you want something like:

/\D[0-1]?\d$/g

The \D will match any non-digit, \d will match any number and you want to optionally (?) allow 0 or 1 in front.

1 Comment

but if i enter something like text#100 i will get last two zeros. I need to reduce everything that is not in 0..19 range.
1

you have to set the character before as a non number or a #, someething like

[^0-9]([0-9]{1}$)|([0-1]{1}[0-9]{1}$)

Comments

0
function getThatThing(x) {
  var match = /#([0-9]|[01][0-9])$/.exec("input#1");
  if (match) {
    return match[1];
  } else {
    return null;
  }
}

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.