Regex
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2024 04:26 AM
Hi All,
I am using regex to get a alphanumeric value from a string . The regex i used is [A-Za-z0-9] . Please help me on this .
Thanks in advance.
15 REPLIES 15
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 04:01 AM
Hi @kali,
Please try this below code:
// Define the subject strings
var subject1 = "This is a sample case for A1BCDD2F(2000-07-03)";
var subject2 = "This is a sample case for 89076523";
// Regular expression for alphanumeric values (letters and digits)
var alphanumericRegex = /\b[a-zA-Z0-9]{8,}\b/g;
// Regular expression for numeric values
var numericRegex = /\b\d+\b/g;
// Function to extract the alphanumeric value from a text
function extractAlphanumeric(text) {
var matches = text.match(alphanumericRegex) || [];
// Return the first alphanumeric match
return matches.length > 0 ? matches[0] : 'No alphanumeric value found';
}
// Function to extract the numeric value from a text
function extractNumeric(text) {
var matches = text.match(numericRegex) || [];
// Return the first numeric match
return matches.length > 0 ? matches[0] : 'No numeric value found';
}
// Extract values from each subject string
var alphanumericValue = extractAlphanumeric(subject1);
var numericValue = extractNumeric(subject2);
// Output the results
gs.info('Extracted Alphanumeric Value from subject1: ' + alphanumericValue);
gs.info('Extracted Numeric Value from subject2: ' + numericValue);
Please accept my solution if works for and thumps up.
Thanks
Jitendra
Please accept my solution if it works for and thumps up.