"One entity object cannot be referenced by multiple IEntityChangeTracker instances" in the ASP. NET MVC Learning Record

Source: Internet
Author: User

Text: A brief talk on the test method of JS regular

In fact, I seldom use this, so I have not been aware of this problem, since the leaves of the man wrote a perverted test I went to see this thing.
Let's take a look at something first.

?
1 2 3) 4 5 var re = /\d/; console.log( re.test("1") ); console.log( re.test("1") ); console.log( re.test("1") );console.log( re.test("1") );

All is true no problem.

But you put /\d/; Change into /\d/g; Try again.

Modify again:

?
1 2 3 4 console.log( /\d/g.test("1") ); console.log( /\d/g.test("1") ); console.log( /\d/g.test("1") );console.log( /\d/g.test("1") );

All is true, what exactly is this for?

These results are quite interesting, of course, the master naturally know why, if you know, the following can actually skip without looking, all is hydrology only.

There is a LastIndex attribute in the regular, which is the starting position of the next match.

?
1 2 3) 4 5 var re = /\d/g; console.log( re.test("1"), re.lastIndex ); console.log( re.test("1"), re.lastIndex ); console.log( re.test("1"), re.lastIndex );console.log( re.test("1"), re.lastIndex );

You can see that the first match result is true to indicate a successful match, at which point the lastIndex records the next match starting at 1.
So the second match of the time from the "1" string index 1 of the position matching, of course, the match failed, because the string has only one character, his index is 0.

and /\d/g. Test ("1") why does each match succeed?
Because it directly uses regular literals, equivalent to recreating a regular object each time, theLastIndex property is the initial value of 0.
So every time we can match the success.

Now is not understood, including exec also, each match one,LastIndex record the next match start position.
If a regular object is to be used, it is only necessary to reset the lastIndex before each test so that he does not get out.

?
1 2 3 4 5 6 7 8 var re = /\d/g; console.log( re.test("1") ); re.lastIndex = 0; console.log( re.test("1") ); re.lastIndex = 0; console.log( re.test("1") ); re.lastIndex = 0;console.log( re.test("1") );

Well, today to modify the syntax highlighting plugin took a lot of time, so water a piece, hope everyone haihan.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.