简体   繁体   English

JavaScript中的正则表达式:在字符串开头匹配多个子字符串吗?

[英]Regular expressions in JavaScript: match multiple substrings at the beginning of a string?

I want to build a JavaScript regular expression to match all words starting with "ad" or "ae", or all words that contain "-ad" and "-ae". 我想构建一个JavaScript正则表达式以匹配所有以“ ad”或“ ae”开头的单词,或所有包含“ -ad”和“ -ae”的单词。

This is what I've tried: 这是我尝试过的:

var regex_string = "^[ad|ae]|-[ad|ae]";
var re = RegExp(regex_string, "i");

var matches = _.filter(data, function(r) {
  if (re.test(r)) {
    return true;
  }
});

However, this is matching all words beginning with 'a', 'd' or 'e'. 但是,这匹配所有以“ a”,“ d”或“ e”开头的单词。

How can I amend the regex to match only those strings? 如何修改正则表达式以仅匹配那些字符串?

JSFiddle here: http://jsfiddle.net/xGgan/1/ JSFiddle在这里: http : //jsfiddle.net/xGgan/1/

Because [] is match any letter inside So it says "match a or d or | or a or e". 因为[]匹配里面的任何字母,所以它说“匹配a或d或|或a或e”。 You need to use a capture group instead with the or. 您需要将捕获组与或一起使用。

Try 尝试

var regex_string = "(^(ad|ae)|-(ad|ae))";

[ ... ] denotes a character class and anything inside will match regardless of position. [ ... ]表示字符类,其中的任何内容都将匹配,而与位置无关。 Additionally, [ae] matches only one character, either a or e . 另外, [ae]仅匹配一个字符,即ae

For what your doing, translating it directly would give: 对于您所做的事情,直接翻译会得到:

(?:^(?:ad|ae)|-(?:ad|ae))

You use | 您使用| in groups ( ( ... ) for capture groups and (?: ... ) for non-capture groups; the latter are preferable if you don't intend to save captures for later use, as they improve the regex speed wise and memory wise). ( ... )代表捕获组, (?: ... )代表非捕获组;如果您不打算保存捕获以备后用,则后者是可取的,因为它们可以提高regex的速度,并明智的记忆)。

But that can be optimised a bit: 但这可以进行一些优化:

(?:^|-)a[ed]

should match just as fine. 应该匹配一样好。

这是我对“ -ae AND -ad”的解释:

/^(?:ad|ae)|-ad.*-ae|-ae.*-ad/

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

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