如何在 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' ]

更新日期: 14-Sep-2020

56 次檢視

開啟你的 職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.