简体   繁体   English

正则表达式匹配?

[英]Regex string match?

I have a long string in javascript like 我在javascript中有一个很长的字符串

var string = 'abc234832748374asdf7943278934haskhjd';

I am trying to match like 我想要匹配

abc234832748374 - that is - I have tried like abc234832748374 - 就是 - 我试过了

string.match(\\abc[^abc]|\\def[^def]|) but that doesnt get me both strings because I need numbers after them ? string.match(\\abc[^abc]|\\def[^def]|)但是这并不能让我得到两个字符串,因为我需要它们之后的数字?

Basically I need abc + 8 chars after and def the 8-11 chars after ? 基本上我需要abc后+ 8个字符和def后的8-11个字符? How can I do this ? 我怎样才能做到这一点 ?

If you want the literal strings abc or def followed by 8-11 digits, you need something like: 如果你想要文字字符串abcdef后跟8-11位数字,你需要这样的东西:

(abc|def)[0-9]{8,11}

You can test it here: http://www.regular-expressions.info/javascriptexample.html 你可以在这里测试一下: http//www.regular-expressions.info/javascriptexample.html

Be aware that, if you don't want to match more than 11 digits, you will require an anchor (or [^0-9] ) at the end of the string. 要知道,如果你不想匹配多于 11位,您将需要一个锚(或[^0-9]在字符串的结尾)。 If it's just 8 or more, you can replace {8,11} with {8} . 如果它只是8或更多,您可以用{8}替换{8,11} {8}

To elaborate on an already posted answer, you need a global match, as follows: 要详细说明已发布的答案,您需要全局匹配,如下所示:

var matches = string.match(/(abc|def)\d{8,11}/g);

This will match all subsets of the string which: 这将匹配字符串的所有子集:

  • Start with "abc" or "def". 以“abc”或“def”开头。 This is the "(abc|def)" portion 这是“(abc | def)”部分
  • Are then followed by 8-11 digits. 然后是8-11位数。 This is the "\\d{8,11}" portion. 这是“\\ d {8,11}”部分。 \\d matches digits. \\ d匹配数字。

The "g" flag (global) gets you a list of all matches, rather than just the first one. “g”标志(全局)为您提供所有匹配的列表,而不仅仅是第一个匹配。

In your question, you asked for 8-11 characters rather than digits. 在你的问题中,你要求8-11个字符而不是数字。 If it doesn't matter whether they are digits or other characters, you can use "." 如果它们是数字还是其他字符无关紧要,您可以使用“。” instead of "\\d". 而不是“\\ d”。

I also notice that each of your example matches have more than 11 characters following the "abc" or "def". 我还注意到,每个示例匹配在“abc”或“def”之后都有超过11个字符。 If any number of digits will do, then the following regex's may be better suited: 如果有任意数量的数字,那么以下正则表达式可能更适合:

  • Any number of digits - var matches = string.match(/(abc|def)\\d*/g); 任意数量的数字 - var matches = string.match(/(abc|def)\\d*/g);
  • At least one digit - var matches = string.match(/(abc|def)\\d+/g); 至少有一个数字 - var matches = string.match(/(abc|def)\\d+/g);
  • At least 8 digits - var matches = string.match(/(abc|def)\\d{8,}/g); 至少8位 - var matches = string.match(/(abc|def)\\d{8,}/g);

You can match abc[0-9]{8} for the string abc followed by 8 digits. 您可以将abc[0-9]{8}与字符串abc匹配,后跟8位数字。

If the first three characters are arbitrary, and 8-11 digits after that, try [az]{3}[0-9]{8,11} 如果前三个字符是任意的,之后是8-11个数字,请尝试[az]{3}[0-9]{8,11}

Use the below regex to get the exact match, 使用下面的正则表达式来获得完全匹配,

string.match(/(abc|def)\d{8,11}/g);

Ends with g 结束与g

"g" for global 全球的"g"

"i" for ignoreCase "i"代表ignoreCase

"m" for multiline "m"代表多行

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

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