If you take a piece of code like this:
var string = "javascript";
var search = string.search("");
console.log(search);
and then you take this piece of code:
var string = "javascript";
var search = string.search("j");
console.log(search);
both pieces of code evaluate to 0. It's fairly annoying since I would imagine it should amount to -1 since there is no match, nothing and the. This is a problem in a script I am trying to write because these both evaluate to the exact same number even though they aren't the same value and this is making me move all my data in my .json file over by one space from this:
"heman",
"candy",
"powerpuffgirls",
"oranges"
to this:
" heman",
" candy",
" powerpuffgirls",
" oranges"
because when I test for a match from the input field the user enters to the .json data I have no way of evaluating whether the data was blank or one matching character was entered (since the javascript search method evaluates both as the same amount '0'). This incapacity of javascript to know the difference between a 'nothing' and a 'single matching char' moved along at zero index (since it's a single matching char) has forced me to move all of .json data over one space.
This feels...wrong to do, like not good practice. It's a solution but it seems like a lame one. Does anybody know any work arounds for a problem like this? Seems like a pretty simple common problem. Is there some way through this, perhaps with regular expressions or something?