-2

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>
Maxim Zhukov
  • 9,627
  • 5
  • 37
  • 78
  • 2
    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. – Pointy Dec 19 '15 at 16:08

2 Answers2

1

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.

Pointy
  • 371,531
  • 55
  • 528
  • 584
1

^ 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.

nicael
  • 16,503
  • 12
  • 50
  • 80