Reputation: 622
I create a function to check if string have a good French social number format
function isValidNoSecu(Tel) { return new RegExp(/[12]\s{1}\d\d\s{1}\d\d\s{1}\d\d\s{1}\d\d\d\s{1}\d\d\d\s{1}|\s{1}\d\d$/i).test(Tel); }
The string must have this form : X XX XX XX XXX XXX | XX First X is 1 or 2 Nothing other I need space (this is my biggest problem) I try \s or \s{1} or [ ] always accepting no space... and I want a space. In fact, the best response will be with just one space caracter not tab... then \s not good Other X are digits : \d
I read lot of tutos, posts and try several tester but I don't find good regex Thanks for your help
Upvotes: 2
Views: 579
Reputation: 27217
Use a space character to indicate matching the (space) character literally.
You can also use curly braces after a character to specify the number of matches required, e.g. \d{2}
means match \d
twice only.
And finally, you need to escape the pipe |
character (using a backslash, like \|
) because it is a special character, meaning OR
.
Like this:
const re = /[12] \d{2} \d{2} \d{2} \d{3} \d{3} \| \d{2}/
console.log(re.test("1 11 22 33 444 555 | 66"))
console.log(re.test("2 33 44 55 666 777 | 88"))
Upvotes: 2