Skip to content
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