[英]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 我需要找到以下内容
I977058-
I977058-
I
can be also D
like D977058-
I
可以也D
喜欢D977058-
zzzz-sxzzukerrdco86rg-CMafApp-java
is not valid zzzz-sxzzukerrdco86rg-CMafApp-java
无效 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.