I have a requirement to find and return the first occurrence of a pattern from a string.
Example: hello my SNO is 05FK3NEK900058V please add it
expected output: 05FK3NEK900058V
Condition for the pattern to match
- Combination of digits and alphabets
- Character length between 12 to 16
Can I do it using regular expression or do I need to loop thought the words?
What I tried but it didn't work
const regex = /\b^(?!\d*$|[a-zA-Z]*$)[a-zA-Z\d]{12,16}$\b/gm; const str = `hello my SNO is 05FK3NEK900058V please add it `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }