How to match strings that aren't entirely digits in JavaScript?


Let’s say the following is our complicated string −

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

You can use regular expression to match strings. Following is the code −

Example

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));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo187.js.

Output

This will produce the following output −

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' ]

Updated on: 14-Sep-2020

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements