Searching Integer value in the string

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 03:39 AM - edited ‎09-27-2023 03:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 05:58 AM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 06:41 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2023 05:50 AM
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.