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