Searching Integer value in the string

Community Alums
Not applicable

Hi Experts,

I need a java script where I should parse and get the Integer value from the string.
below are the string example,
1.Dell Latitude 1234
2.Latitude Dell 4321
3.Dell Latitude (500GB) 6789

4.Dell Latitude Longitude 5376

From the above examples the script has to pick the Integer values i.e "1234" from 1st, "4321" from 2nd, "6789" from 3rd and "5376" from 4th.. these are some different  string examples. but integer could be any where with in the string.

Kindly suggest me on this.

Thanks in advance,
Chaithanya

7 REPLIES 7

HI @Community Alums ,
try this it will work.

var inputString1 = "Dell Latitude 1234";
var inputString2 = "Dell Latitude (500GB) 6789";

var regex = /\d+(?![\d\D]*\d)/;

var match1 = inputString1.match(regex);
var match2 = inputString2.match(regex);

var intValue1 = match1 ? parseInt(match1[0]) : null;
var intValue2 = match2 ? parseInt(match2[0]) : null;

gs.print(intValue1); 
gs.print(intValue2); 

Community Alums
Not applicable

Hi @Anand Kumar P 
Thank you! it worked.
But there is one more scenario , if the string is "dell 7440E latitude" can we print the output as "7440E" instead of just "7440".

Thanks!

Anubhav24
Mega Sage
Mega Sage

Hi ,

Can you confirm that the numbers will always be numeric and not mixed with strings like 500GB ? Also if the numbers are at last you do an extract from the last position till first space encountered or use a regexp.