สมมติว่าเรามีสตริงต่อไปนี้ หนึ่งในนั้นเริ่มต้นด้วยเครื่องหมายคำถาม เช่น เครื่องหมายวรรคตอน -
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