驗證字串是否以 JavaScript 中的標點符號開頭
假設我們有下列字串。其中一個以問號開頭,即標點符號 −
var sentence1 = 'My Name is John Smith.' var sentence2 = '? My Name is John Smith.'
我們需要檢查上述兩個句子是否以標點符號開頭。若要檢查字串是否以標點符號開頭,程式碼如下 −
示例
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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP