如何在 JavaScript 中匹配並非完全由數字組成的字串?
假設下面是我們複雜的字串 -
var values = 'studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678"';
你可以使用正則表示式匹配字串。以下為程式碼 -
示例
var regularExpression = /(?<=:)(?!(?:null|false|true|\d+),)[\w-]+(?=,)/g;
var values = 'studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678"';
console.log("Original Value="+values);
console.log("The string that are not entirely digits=");
console.log(values.match(regularExpression));要執行以上程式,你需要使用以下命令 -
node fileName.js.
在此,我的檔名是 demo187.js。
輸出
這將產生以下輸出 -
PS C:\Users\Amit\javascript-code> node demo187.js Original Value=studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678" The string that are not entirely digits= [ '10001J-10001' ]
廣告
資料結構
聯網
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP