String is Dynamic.Example
Date18:Month5:Year1984
How can we extract just number 18. I cannot use substring because the values are dynamic.I want to extract only the number after Date and before :
String is Dynamic.Example
Date18:Month5:Year1984
How can we extract just number 18. I cannot use substring because the values are dynamic.I want to extract only the number after Date and before :
Here is what you want:
const a = "Date18:Month5:Year1984";
const match = a.match(/^Date([0-9]+?):/);
if(match){
console.log(match[1]);
}
regular expressions are great for matching patterns. There are many ways you can write it, here is a couple ways.
var str = "Date18:Month5:Year1984"
const re = /^Date(\d+):Month(\d+):Year(\d+)$/;
const match = str.match(re);
console.log(match[1], match[2], match[3]);
const re2 = /(\d+)/g;
const match2 = str.match(re2);
console.log(match2[0], match2[1], match2[2]);