Reputation: 9303
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
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}`);
});
}
Upvotes: 0
Views: 73
Reputation: 2315
After @bobble buble comment
I think this will works:
\w{12,16}
Also you can use words boundaries if you match required to be a word.
\b\w{12,16}\b
Updated after @PeterM Comment:
The last version more verbose but more accurate:
string=`Johannsonoff 111111111111111 05FK3NEK900058Vf 05FK3NEK900058Vfaf48 hello my SNO is fFK3NEK9000515 05FK3NEK9000515 please add it 05FK3NEK900058Vfaf48 faf fasf1654 EK90058Vs11 EK9005f8Vs12 05FK3NE90058Vs16 05FK3NEK90058Vs17`;
// Returns an Array in which elements are strings between 12 and 16 characters.
const groupMathed = string.match(/\b\w{12,16}\b/g)
// Returns the first element that have a digit [0-9] follow by a alphabetic character or a alphabetic character followed by a digit.
const alphanumericWord = groupMathed.find(e => /[0-9]+[a-zA-z]+|[a-zA-z]+[0-9]+/.test(e))
console.log(alphanumericWord)
Another approach can be use this regex which is more verbose but reduce the code:
(?=(\b\d+[a-zA-Z]|\b[a-zA-Z]\d+))[a-zA-Z0-9]{12,16}\b
Demo: https://regex101.com/r/0lMyoV/11/
string=`asdfghjlqerut hello my SNO is 1234567891234 05FK3NEK900058V 0FK3NEK900058V asdfasfd25fasfaa please add it 05FK3NEK900058Vfaf48 faf fasf1654 F0K3NEK900058V Johannsonoff 111111111111111 05FK3NEK900058Vf 05FK3NEK900058Vfaf48 hello my SNO is fFK3NEK9000515 05FK3NEK9000515 please add it 05FK3NEK900058Vfaf48 faf fasf1654 EK90058Vs11 EK9005f8Vs12 05FK3NE90058Vs16 05FK3NEK90058Vs17 05FK3NEK900058Vfaf48`;
const regex = /(?=(\b\d+[a-zA-Z]|\b[a-zA-Z]\d+))[a-zA-Z0-9]{12,16}\b/g
const groupMathed = string.match(regex)
console.log(groupMathed)
Upvotes: 3
Reputation: 562
let str = "hello (plus SomeLongWord) my SNO is 05FK3NEK900058V please add it ";
const reg = /\b[\w\d]{12,16}\b/g;
let m = str.match(reg);
if (typeof m === "object" && m.length) {
for (var indexM = 0; indexM < m.length; indexM++) {
// You can add more checks to filter long words
console.log(m[indexM]);
}
} else {
console.log("Not found any SNO here");
}
// results:
// SomeLongWord
// 05FK3NEK900058V
That will help you to handle long words but probably more check will be needed.
Upvotes: 0
Reputation: 627468
You may use a single regex like
/\b(?=[a-zA-Z\d]{12,16}\b)(?:[a-zA-Z]*\d|\d*[a-zA-Z])[a-zA-Z\d]*\b/
See the regex demo.
Details
\b
- word boundary(?=[a-zA-Z\d]{12,16}\b)
- up to the next word boundary, there must be 12 through 16 alphanumeric chars(?:[a-zA-Z]*\d|\d*[a-zA-Z])
- either 0+ letters and a digit or 0+ digits and then a letter[a-zA-Z\d]*
- 0+ alphanumeric chars\b
- word boundary.See a JS demo below:
var str = "hello my SNO is 05FK3NEK900058V please add it ";
var reg = /\b(?=[a-zA-Z\d]{12,16}\b)(?:[a-zA-Z]*\d|\d*[a-zA-Z])[a-zA-Z\d]*\b/;
var m = str.match(reg) || [""];
console.log(m[0]);
Upvotes: 2