Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b5f17b4

Browse files
committed
WIP
1 parent 32e20fc commit b5f17b4

File tree

1 file changed

+5
-15
lines changed
  • 9-regular-expressions/17-regexp-methods

1 file changed

+5
-15
lines changed

‎9-regular-expressions/17-regexp-methods/article.md‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ alert( *!*/love/i*/!*.test(str) ); // false
316316
alert( str.search(*!*/love/i*/!*) != -1 ); // false
317317
```
318318
319-
If the regexp has flag `pattern:g`, then `regexp.test` looks from `regexp.lastIndex` property and updates it, just like `regexp.exec`.
319+
If the regexp has flag `pattern:g`, then `regexp.test` looks from `regexp.lastIndex` property and updates this property, just like `regexp.exec`.
320320
321321
So we can use it to search from a given position:
322322
@@ -326,13 +326,11 @@ let regexp = /love/gi;
326326
let str = "I love JavaScript";
327327

328328
// start the search from position 10:
329-
regexp.lastIndex = 10
329+
regexp.lastIndex = 10;
330330
alert( regexp.test(str) ); // false (no match)
331331
```
332332
333-
334-
335-
````warn header="Same global regexp tested repeatedly may fail to match"
333+
````warn header="Same global regexp tested repeatedly on different sources may fail"
336334
If we apply the same global regexp to different inputs, it may lead to wrong result, because `regexp.test` call advances `regexp.lastIndex` property, so the search in another string may start from non-zero position.
337335
338336
For instance, here we call `regexp.test` twice on the same text, and the second time fails:
@@ -344,15 +342,7 @@ alert( regexp.test("javascript") ); // true (regexp.lastIndex=10 now)
344342
alert( regexp.test("javascript") ); // false
345343
```
346344
347-
That's exactly because `regexp.lastIndex` is non-zero on the second test.
345+
That's exactly because `regexp.lastIndex` is non-zero in the second test.
348346
349-
To work around that, one could use non-global regexps or re-adjust `regexp.lastIndex=0` before a new search.
347+
To work around that, we can set `regexp.lastIndex=0` before each search. Or instead of calling methods on regexp, use string methods `str.match/search/...`, they don't use `lastIndex`.
350348
````
351-
352-
## Summary
353-
354-
There's a variety of many methods on both regexps and strings.
355-
356-
Their abilities and methods overlap quite a bit, we can do the same by different calls. Sometimes that may cause confusion when starting to learn the language.
357-
358-
Then please refer to the recipes at the beginning of this chapter, as they provide solutions for the majority of regexp-related tasks.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /