2016-06-03 1 views
-2

사용자가 a-z를 입력 할 수 있지만 0-9를 추가하고 대시하는 방법은 무엇입니까? /^[a-zA-Z\s]*$/;은 1-9와 대시 정규식을 허용합니다

+0

당신이 사용자가 [공백]을 입력 할 수는 ? – weigreen

+1

정규 표현식을 사용하는 방법에 대한 자습서를 읽었습니까? 이것은 매우 보편적으로 다루어지며 기본적입니다. 또한 0-9 또는 1-9를 다루는 것을 의미합니까, 질문의 제목과 문맥은 다양합니다. –

+0

@SpencerWieczorek 나는'-'을 어디에 넣어야할지 모르겠습니다. –

답변

1
/^[a-zA-Z\d\s-]*$/ 
마지막 문자 클래스의 ( [])에서
  1. 대시 , 그렇지 않으면 당신이 0-9하거나 \d 사용할 수 있습니다 숫자를 일치 시키려면 \-
  2. 를 사용하여 이스케이프 할 필요가 없습니다 당신의 정규식 풍미에 따라.

정규식 설명 :

^[a-zA-Z\d\s-]*$ 

Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^» 
Match a single character present in the list below «[a-zA-Z\d\s-]*» 
    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    A character in the range between “a” and “z” (case insensitive) «a-z» 
    A character in the range between “A” and “Z” (case insensitive) «A-Z» 
    A “digit” (any decimal number in any Unicode script) «\d» 
    A “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) «\s» 
    The literal character “-” «-» 
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$» 
+0

'\ s'은 무엇을위한 것입니까? '\ s @'할 수 있습니까? –

+0

@HarisZ ['\ s'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-white-space)는 하나의 공백 문자입니다. 당신은 무엇인가의 이유로''[white space char] @ "'와 일치하는 것을 찾고 있다면 할 수 있습니다. –

+0

'\ s' 공백 문자 (유니 코드 구분 기호, 탭, 줄 바꿈, 캐리지 리턴, 세로 탭, 양식 피드, 다음 줄)와 일치 –

0

당신은 /^[1-9-]*$/

여기에 자바 스크립트 예제를 찾고 :

var reg = new RegExp("^[1-9-]*$"); 
var s = '1234-5678'; 
if (reg.exec(s)) { 
    console.log("Match\n"); 
} else { 
    console.log("No match\n"); 
} 
+0

'= ~'무엇입니까? 처음으로 나는 이것을 보았습니다 –

+0

Perl에서는'$ s' 변수의 값이 정규 표현식과 일치하는지 테스트하고 있습니다. – user212514

+0

Perl의 예제가 제공된 이유가 확실하지 않습니다. JavaScript 예제가 더 적절할 것입니다. –

관련 문제