正则表达式[^j)和匹配结果 - Regular expression [^A-J] and match result

- 此内容更新于:2015-12-20
主题:

你能解释,为什么下匹配输出的结果吗?更新:我认为输出应该在正常范围字符匹配。

原文:

Could you please explain, why output result of next match is s?

Update: I thought that output should be I as matched in regular range character.

var str = "Is this enough?";
var patt1 = new RegExp("[^A-J]");
var result = str.match(patt1); // ["s", index: 1, input: "Is this enought?"]
document.getElementById('id').innerHTML = result;
<p id="id"></p>

网友:这将帮助如果你能解释一下你的预期结果;事实上,没有人知道它是什么,你认为需要解释。

(原文:It would help if you could explain what you expected the result to be; as it is, nobody knows what it is that you think requires explanation.)

解决方案:
你的正则表达式匹配任何字符的范围之外通过大写字母的大写,所以第一个字符,满足要求。像你这样的一个正则表达式匹配搜索字符串的任何地方。更清晰,你的角色组表达式的开始意味着不描述应该包括所有的字符组的内容。你的范围,所以匹配所有字符除了通过J。
原文:

Your regular expression matches any character outside the range of upper-case A through upper-case J, and so s is the first character that meets that requirement. A regular expression like yours will match anywhere in the search string.

To be more clear, the ^ at the start of your character group expression means that the group should include all characters not described by the contents of the [^ ]. Your range is A-J, so [^A-J] matches all characters except A through J.

楼主:我有点困惑匹配范围之外

(原文:I'm a little bit confused about matching outside range)

网友:@FSou1我不明白你的意思。你期望的模式匹配什么?

(原文:@FSou1 I don't understand what you mean. What did you expect the pattern to match?)

楼主:我不知道为什么你的回复关于匹配范围以外的任何字符。看起来像那是因为有

(原文:I have no idea why do you reply about 'matching any character outside the range'. Looks like that's because there is ^)

网友:@FSou1是的,这正是主要的意思。如果你想匹配,离开^。

(原文:@FSou1 yes, that is exactly what that leading ^ means. If you want to match A through J, leave off the ^.)

楼主:太好了,谢谢帮忙

(原文:Great, thx for help)

解决方案:
意味着“匹配的字符不是设置下列字符”。因为你还没有指定任何旗帜,它只返回第一个匹配。第一个匹配,它不是在的字符集J。
原文:

^ means "match the char that is not in the set of the following characters". Because you haven't specified any flags, it returns only the first match. The first match is s, which is not in the set of characters A to J.

楼主:并不意味着匹配任何字符在字符串的开始?

(原文:Doesn't ^ means to match any character in set at start of the string?)

网友:不是在一个字符类不

(原文:Not inside a character class it doesn't)

网友:@FSou1没有——在正则表达式中有两个不同的角色。当它开始时的一个群体,这意味着匹配组的补充。你在想的^[]外模式的开始。

(原文:@FSou1 no - the ^ has two different roles in regular expressions. When it's at the start of a [ ] group, it means to match the complement of that group. You're thinking of ^ at the start of a pattern outside [ ].)

网友:我迷惑的样子

(原文:Looks like i confuse /^A/ and /[^A]/)

网友:Thx很多@nicael

(原文:Thx a lot @nicael)