If I'm comparing a string with the correct spelling of "Honolulu" I'd like to know that the first mistake in "Hnolulo" is the missing first o, i.e. character 1.
Asked
Active
Viewed 59 times
-2
-
Amazing to see people trying to solve almost every programming problem using regex. – anubhava Apr 10 '14 at 17:42
-
... which is actually [possible](http://stackoverflow.com/questions/3296050/how-does-this-regex-find-primes). – raina77ow Apr 10 '14 at 17:51
-
2@raina77ow if you use unary, that is. – John Dvorak Apr 10 '14 at 17:52
1 Answers
1
It's not regex, but will work
var correct = "Honolulu";
var input = "Hnolulu";
var v = validate(correct, input); // 1
function validate(correct, input) {
var error = -1;
var comp1 = correct.length > input.length ? correct : input;
var comp2 = correct.length > input.length ? input : correct;
for (var i=0; i<comp1.length; i++) {
if (comp1.charAt(i) != comp2.charAt(i)) {
error = i;
break;
}
}
return error;
}

adeneo
- 312,895
- 29
- 395
- 388