I am new to regular expressions and would like to know how to write a regular expression that does the following:
Checks a string like 123-0123456789. Only numeric values and hyphens are allowed. Also, make sure that there are 3 numeric characters and 10 characters after the hyphen before the hyphen.
These answers will not work for strings with a large number of digits (for example, "012-0123456789876"), so you need to:
str.match(/^\d{3}-\d{10}$/) != null;
or
/^\d{3}-\d{10}$/.test(str);
Try the following:
^\d{3}-\d{10}$
It says: Accept only 3 digits, then a hyphen, then only 10 digits
, :
var valid = (str.match(/^\d{3}-\d{10}$/) != null);
:
> s = "102-1919103933"; "102-1919103933" > var valid = s.match(/\d{3}-\d{10}/) != null; > valid true > s = "28566945"; "28566945" > var valid = s.match(/\d{3}-\d{10}/) != null; > valid false
Source: https://habr.com/ru/post/1792978/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1792973/entity-framework-4-and-repository-pattern&usg=ALkJrhiPMeDb-8RR7k6K9HBI_ANNRfxm1QWhy is the wrong function performed? - c ++Replace consecutive repeated tags with one of these tags in Ruby - ruby | fooobar.comcode for printing with i-pad and which all printing supports - iphoneNSOutlineView Change Image Deployment - objective-cЕсть ли способ добавить в iCal идентификатор или тег пользовательского события? - iphoneWorkflows on Linux - linuxC # Windows Forms application update on application launch - c #pop-up message in android - androidAdd custom action to VS 2008 installation project - c #All Articles