i'm running into an issue where i'm struggling on the logic behind parsing for 1-XXX-XXXX of a users response, where XXX are integer values.
Asked
Active
Viewed 1,052 times
-2
-
3Have you [**tried anything**](http://meta.stackoverflow.com/questions/261592) so far? – Obsidian Age Sep 03 '19 at 21:53
-
I've attempted a bit of regex, but i'm not well versed at it whatsoever. I am able to do XXX-XXXX in regex – user8738587 Sep 03 '19 at 21:56
-
1If you've tried a regex solution already, consider adding your existing code into your question. By showing us what you've tried so far, we can point out how to change what you already have to make it work. StackOverflow isn't a free coding service--we're (usually) not going to write your code for you, but we will almost certainly help you fix your existing code. – B. Fleming Sep 03 '19 at 22:00
-
1Hint: "X" translates to `\d` in regular expression terms. – tadman Sep 03 '19 at 22:00
-
Can you post the regex where you did the validation of XXX-XXXX? – Sergey Sep 03 '19 at 22:02
2 Answers
0
This regex match should get you what you're looking for
let regex = /1-[0-9]{3}-[0-9]{3}-[0-9]{4}/

cullanrocks
- 457
- 4
- 16
-1
Try
let candidateValue = getMeSomeValue();
const isValid = (candidateValue || "").match( /^1-[0-9]{3}-[0-9]{4}$/ );
Add \s
following the ^
and before the $
if you want to play nice and ignore leading/trailing whitespace.

Nicholas Carey
- 71,308
- 16
- 93
- 135
-
-
-
there's a `d` typo too, I'd go with `/^1-\d{3}-\d{4}$/.test(candidateValue.trim())` and call it a day – Andrea Giammarchi Sep 03 '19 at 22:02
-
`\d` covers things other than ASCII `0-9`. It matches the *entire* Unicode General Category `Nd`. Any decimal digit across any language. Here's the set of characters it matches: https://www.fileformat.info/info/unicode/category/Nd/list.htm — and see this question, https://stackoverflow.com/questions/16621738/d-is-less-efficient-than-0-9, for why you might want to prefer `[0-9]` over `\d` (unless you intend to accept things like Arabic, Bengali or Lao digits). – Nicholas Carey Sep 06 '19 at 22:03