Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách khớp chuỗi không hoàn toàn là chữ số trong JavaScript

Giả sử sau là chuỗi phức tạp của chúng ta:

var values = 'studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678"';

Bạn có thể sử dụng biểu thức chính quy để khớp chuỗi. Dưới đây là mã:

Ví dụ

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("Giá trị gốc=" + values);
console.log("Chuỗi không hoàn toàn là chữ số=");
console.log(values.match(regularExpression));

Để chạy chương trình trên, bạn cần sử dụng lệnh sau:

node fileName.js

Tên tệp của tôi là demo187.js.

Kết quả

Chương trình sẽ cho ra kết quả sau:

PS C:\Users\Amit\javascript-code> node demo187.js
Giá trị gốc=studentId:"100001",studentName:"John Smith",isTopper:true,uniqueId:10001J-10001,marks:78,newId:"4678"
Chuỗi không hoàn toàn là chữ số=
[ '10001J-10001' ]