简体   繁体   English

在字符串正则表达式中查找模式

[英]Find pattern inside string regex

I need to find if some pattern is exist in the following string 我需要查找以下字符串中是否存在某种模式

I123456-sxzzukerrdco86rg-CMafApp-java

Or D977058-sxzzukerrdco86rg-CMafApp-java D977058-sxzzukerrdco86rg-CMafApp-java

I need to find the following 我需要找到以下内容

  1. if string start with I977058- 如果字符串以I977058-
  2. The I can be also D like D977058- I可以也D喜欢D977058-
  3. the string should be find in the start of the string like I977058-sxzzukerrdco86rg-CMafApp-java , it cannot be in any other part of the string… 该字符串应该在字符串的开头找到,例如I977058-sxzzukerrdco86rg-CMafApp-java,不能在字符串的任何其他部分…
  4. zzzz-sxzzukerrdco86rg-CMafApp-java is not valid zzzz-sxzzukerrdco86rg-CMafApp-java无效
  5. 11111-sxzzukerrdco86rg-CMafApp-java not valid 11111-sxzzukerrdco86rg-CMafApp-java无效

I try with the following which doesn't works 我尝试以下无效

I don't know how to provide pattern which take also the D or I as starting point 我不知道如何提供以D或I为起点的模式

var res = str.match(/I-/g);

不能完全清楚您要匹配的内容,但是以下模式将匹配任何以“ D”或“ I”开头的字符串-^字符表示字符串的开头。

var res = str.match(/^[ID]/);

You could check against the two letters and the wanted digits and a dash. 您可以对照两个字母,想要的数字和破折号。

 console.log([ 'I123456-sxzzukerrdco86rg-CMafApp-java', 'D977058-sxzzukerrdco86rg-CMafApp-java', 'foo' ].map(/./.test.bind(/^[DI]\\d{6}-/))); 

Try something like that : 试试这样的事情:

/^[ID]/

The '^' tells that it is the beginning of the word. '^'表示这是单词的开头。 [ID] => get a char in group {I,D} [ID] =>在{I,D}组中获取一个字符

You can use this website to train and understand regexpressions : https://regex101.com/ 您可以使用此网站来训练和理解正则表达式: https ://regex101.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM