簡體   English   中英

字母數字詞的正則表達式

[英]Regular expression for an alphanumeric word

我需要從字符串中查找並返回模式的首次出現。

示例 :您好,我的SNO是05FK3NEK900058V,請添加

預期輸出 :05FK3NEK900058V

模式匹配的條件

  1. 數字和字母的組合
  2. 字符長度在12到16之間

我可以使用正則表達式嗎?還是需要循環使用這些單詞?

我試過了但是沒用

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}`);
    });
}

@bobble buble評論后

我認為這會起作用:

\w{12,16}

演示版

如果您需要匹配一個單詞,也可以使用單詞邊界。

\b\w{12,16}\b

演示:

@PeterM后更新評論:

最后一個版本更詳細但更准確:

 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) 

另一種方法可以使用此正則表達式,它較為冗長,但可以減少代碼

(?=(\b\d+[a-zA-Z]|\b[a-zA-Z]\d+))[a-zA-Z0-9]{12,16}\b

演示: 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) 

您可以使用單個正則表達式

/\b(?=[a-zA-Z\d]{12,16}\b)(?:[a-zA-Z]*\d|\d*[a-zA-Z])[a-zA-Z\d]*\b/

參見regex演示

細節

  • \\b單詞邊界
  • (?=[a-zA-Z\\d]{12,16}\\b) -直到下一個單詞邊界,必須有12到16個字母數字字符
  • (?:[a-zA-Z]*\\d|\\d*[a-zA-Z]) -0+個字母和一個數字或0+個數字然后一個字母
  • [a-zA-Z\\d]* -0+個字母數字字符
  • \\b單詞邊界。

請參見下面的JS演示:

 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]); 

 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 

這將幫助您處理長單詞,但可能需要更多檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM