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

Add support to regular expressions using a global parameter. #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ var Highlighter = createReactClass({
getMatchBoundaries: function(subject, search) {
var matches = search.exec(subject);
if (matches) {
search.lastIndex = 0;
return {
first: matches.index,
last: matches.index + matches[0].length
Expand Down Expand Up @@ -172,6 +173,7 @@ var Highlighter = createReactClass({
children.push(this.renderPlain(remaining));
return children;
}
search.lastIndex = 0;

var boundaries = this.getMatchBoundaries(remainingCleaned, search);

Expand Down
8 changes: 8 additions & 0 deletions test/testHighlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ describe('Highlight element', function() {
expect(matches).to.have.length(0);
});

it('should support regular expressions with the global parameter', function() {
var element = React.createElement(Highlight, {search: /brown|fox/g}, 'The quick brown fox jumped over the lazy dog.');
var node = TestUtils.renderIntoDocument(element);
var matches = TestUtils.scryRenderedDOMComponentsWithTag(node, 'mark');
expect(ReactDOM.findDOMNode(matches[0]).textContent).to.equal('brown');
expect(ReactDOM.findDOMNode(matches[1]).textContent).to.equal('fox');
});

it('should stop immediately if regex matches an empty string', function() {
var element = React.createElement(Highlight, {search: /z*/}, 'Ez as 123, ABC...');
var node = TestUtils.renderIntoDocument(element);
Expand Down