0

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?

Allan Socks
  • 261
  • 2
  • 10
  • 1
    The empty string appears at position 0 in the string. See the extremely silly answer to [this old question](http://stackoverflow.com/questions/2683466) for a philosophical take on the matter. – Pointy Oct 14 '14 at 19:46
  • 3
    Why not just check that the search string is not the empty string before searching? – Sean Bright Oct 14 '14 at 19:49
  • FWIW, searching for `.` will also return `0`. – Sean Bright Oct 14 '14 at 19:50
  • 2
    [search](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) is supposed to take a Regexp. when you pass an empty string, it is converted to a Regexp of `/(?:)/` which matches the start of the string. – yoavmatchulsky Oct 14 '14 at 19:53
  • e.g. use [indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) instead. – yoavmatchulsky Oct 14 '14 at 19:54
  • `indexOf` has the same problem (returns `0` when passed the empty string) – Sean Bright Oct 14 '14 at 19:54
  • You're the one calling `search` - surely you can validate what you're searching for before you call it? Also, how does adding a space at the beginning potentially solve anything - it'll still return `0` as the empty string exists everywhere. – Krease Oct 14 '14 at 20:00
  • Actually, if I add a space in front of the .json data it does solve my problem because: var str = " javascript"; var n = str.search(""); evaluates to 0 on a console log and var str = " javascript"; var n = str.search("j"); evaulates to 1 on a console log. whereas: var str = "javascript"; var n = str.search(""); evaluates to ZERO and var str = "javascript"; var n = str.search(""); evaluates to ZERO. So, it absolutely does solve my problem. It's just a crappy solution and I'd rather use something with better practice...hence the question. – Allan Socks Oct 14 '14 at 20:06
  • Are you ignoring the other suggestions? Validate that your "query" (the argument you are passing to `search`) is not the empty string. What is complicated about this? – Sean Bright Oct 14 '14 at 20:12

2 Answers2

1

Remember this: Always, whatever be the value of the variable string (including empty string)

string.search("") = 0 

So, check for length of the string!

if (string.length == 0) // Empty string
Avijit Gupta
  • 5,676
  • 3
  • 20
  • 35
0
var search = query.length === 0 ? -1 : string.search(query);
Sean Bright
  • 118,630
  • 17
  • 138
  • 146