Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use regexp match position instead of indexOf() #38

Closed
wants to merge 1 commit into from
Closed

Use regexp match position instead of indexOf() #38

wants to merge 1 commit into from

Conversation

ChaseWagoner
Copy link

Fixes #36.

Matches were previously found using str.match(regexp), then
added as a <mark> at position str.indexOf(matches[i]).
This led to inaccurate highlights. For example:

  • Text content: "Somewhere here"
  • regexp search /\bhe/g (word boundary followed by 'he')
  • str.match(regexp) correctly returns 'he', but not the position
    of the match
  • str.indexOf('he') reports 5 (in the middle of 'Somewhere')
    instead of 10 ('here')

This is resolved by using regexp.exec(str), which returns the index
of matched text, allowing word boundaries to function correctly (and
likely fixing some other devious but yet-undiscovered bugs).

Matches were previously found using `str.match(regexp)`, then
added as a `<mark>` at position `str.indexOf(matches[i])`.
This led to inaccurate highlights. For example:

- Text content: "Somewhere here"
- regexp search `/\bhe/g` (word boundary followed by 'he')
- `str.match(regexp)` correctly returns 'he', but not the position
  of the match
- `str.indexOf('he')` reports 5 (in the middle of 'Somewhere')
  instead of 10 ('here')

This is resolved by using `regexp.exec(str)`, which returns the index
of matched text, allowing word boundaries to function correctly (and
likely fixing some other devious but yet-undiscovered bugs).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Word boundaries don't seem to work
1 participant