다음과 같은 문자열이 있다고 가정해 보겠습니다. 그 중 하나는 물음표로 시작합니다. 즉, 구두점 -
var sentence1 = 'My Name is John Smith.' var sentence2 = '? My Name is John Smith.'
위의 두 문장 중 구두점으로 시작하는 문장이 있는지 확인해야 합니다. ifstring이 구두점으로 시작하는지 확인하려면 코드는 다음과 같습니다 -
예시
var punctuationDetailsRegularExpression=/^[.,:!?]/
var sentence1 = 'My Name is John Smith.'
var output1 = !!sentence1.match(punctuationDetailsRegularExpression)
if(output1==true)
console.log("This ("+sentence1+") starts with a punctuation");
else
console.log("This ("+sentence1+") does not starts with a punctuation");
var sentence2 = '? My Name is John Smith.'
var output2 = !!sentence2.match(punctuationDetailsRegularExpression)
if(output2==true)
console.log("This ( "+sentence2+") starts with a punctuation");
else
console.log("This ( "+sentence2+" ) does not starts with apunctuation"); 위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -
node fileName.js.
여기 내 파일 이름은 demo209.js입니다.
출력
이것은 다음과 같은 출력을 생성합니다 -
PS C:\Users\Amit\javascript-code> node demo209.js This (My Name is John Smith.) does not starts with a punctuation This ( ? My Name is John Smith.) starts with a punctuation