Performance test of regular expression to match string not containing a word?
HTML
Regular expression to match string not containing a word?
JavaScript
function measure(msg, fn) {
var start, end, el;
start = new Date();
fn();
end = new Date();
el = document.createElement( 'DIV' );
el.innerHTML = msg + ' takes: ' + (end - start) + ' ms';
document.body.appendChild(el);
}
var hihi = 'I know it is possible to match for the word and using tools options reverse the match. (eg. by grep -v) However I want to know if it is possible using regular expressions to match lines which does not contain a specific word, say hihi?';
var hede = 'I know it is possible to match for the word and using tools options reverse the match. (eg. by grep -v) However I want to know if it is possible using regular expressions to match lines which does not contain a specific word, say hede?';
function test(regex, text) {
var i, n;
for ( i = 0, n = 1000000; i < n; ++i ) {
regex.exec(text);
}
}
var accepted = /^((?!hede).)*$/;
measure(accepted + ' test text with "hede"', function() {
test(accepted, hede);
});
measure(accepted + ' test text without "hede"', function() {
test(accepted, hihi);
});
var accepted_nocapture = /^(?:(?!hede).)*$/;
measure(accepted_nocapture + ' test text with "hede"', function() {
test(accepted_nocapture, hede);
});
measure(accepted_nocapture + ' test text without "hede"', function() {
test(accepted_nocapture, hihi);
});
var my = /^(?!.*hede).*$/;
measure(my + ' test text with "hede"', function() {
test(my, hede);
});
measure(my + ' test text without "hede"', function() {
test(my, hihi);
});
var mylazy = /^(?!.*?hede).*$/;
measure(mylazy + ' test text with "hede"', function() {
test(mylazy, hede);
});
measure(mylazy + ' test text without "hede"', function() {
test(mylazy, hihi);
});