Javascript 正则表达式字符串匹配?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10827344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:05:56  来源:igfitidea点击:

Regex string match?

javascriptregexstringmatch

提问by Andy

I have a long string in javascript like

我在 javascript 中有一个很长的字符串,比如

var string = 'abc234832748374asdf7943278934haskhjd';

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 defthe 8-11 chars after ? How can I do this ?

基本上我需要abc+ 8 个字符之后和def8-11 个字符之后?我怎样才能做到这一点 ?

回答by paxdiablo

If you want the literal strings abcor deffollowed 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 morethan 11 digits, you will require an anchor (or [^0-9]) at the end of the string. If it's just 8 or more, you can replace {8,11}with {8}.

要注意的是,如果你不希望匹配超过11位,您将需要一个锚(或[^0-9]字符串的结尾)。如果只是 8 个或更多,则可以替换{8,11}{8}.

回答by Joe Sarre

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". This is the "(abc|def)" portion
  • Are then followed by 8-11 digits. This is the "\d{8,11}" portion. \d matches digits.
  • 以“abc”或“def”开头。这是“(abc|def)”部分
  • 然后是 8-11 位数字。这是“\d{8,11}”部分。\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 charactersrather than digits. If it doesn't matter whether they are digits or other characters, you can use "." instead of "\d".

在您的问题中,您要求输入 8-11 个字符而不是数字。如果不管是数字还是其他字符,都可以用“.”。而不是“\d”。

I also notice that each of your example matches have more than 11 characters following the "abc" or "def". If any number of digits will do, then the following regex's may be better suited:

我还注意到,您的每个示例匹配项在“abc”或“def”之后都有超过 11 个字符。如果任意数量的数字都可以,那么以下正则表达式可能更适合:

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

回答by 0605002

You can match abc[0-9]{8}for the string abcfollowed by 8 digits.

您可以匹配后跟 8 位数字abc[0-9]{8}的字符串abc

If the first three characters are arbitrary, and 8-11 digits after that, try [a-z]{3}[0-9]{8,11}

如果前三个字符是任意的,然后是 8-11 位数字,请尝试 [a-z]{3}[0-9]{8,11}

回答by premnathcs

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"用于多行