2

I am trying to search a string to see if it contains a period "." I loaded the value of an input field into a variable then ran a javascript string search() method on that variable.

var _email = document.getElementById(Input).value;
var contains_dot = _email.search(".");

The contains_dot variable should return -1 if a period is not found in the string OR a number representing the position of the match.

Sadly... the variable 'contains_dot' returns nothing but 0 every time. Am i doing something wrong? ..is there a better way?

Please note:

contains_dot = _email.indexOf(".");

Does NOT work for this question as it returns 0 no matter what the input.

Really Nice Code
  • 1,144
  • 1
  • 13
  • 22

3 Answers3

14

Why does that happen?

The reason why _email.search("."); returns 0 every time is because String.prototype.search takes a regular expression as its input.

. means match any character in regex. In other words, if your input has at least one character of anything, it will return 0.


The Solution

If you simply change it to _email.search(/\./);, it will work exactly as you intended.

Browser support: All known browsers

If you don't care about browser support, you may also use _email.includes('.'); as mentioned by Cade Brown.

See here for reference. Browser support: Only Chrome 41+ and Firefox 40+ (recent browsers)

user193130
  • 8,009
  • 4
  • 36
  • 64
  • Using a regex seems like an overkill for this. `indexOf` is better in that case. – jonathanGB Oct 03 '16 at 02:15
  • Thanks, your snippet of code did exactly what i wanted. And i also learnt something about 'matching any character in regex'. – Really Nice Code Oct 03 '16 at 02:17
  • @ReallyNiceCode Great :) You can check [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#character-classes) for more info on the `.` in JS RegEx. If all you need is to find the dot though, I'd recommend `indexOf` or `includes` as others have mentioned. They should be more efficient in performance. `indexOf` is also compatible with all known browsers. – user193130 Oct 03 '16 at 02:21
  • @jonathanGB Agreed. I wanted to point out why the original code didn't work and also offer a quick fix based on the OP's original reasoning. – user193130 Oct 03 '16 at 02:24
  • I understand, you explained it well. But you should edit your answer to mention `indexOf` and why it should be used over `search` here, and possibly over `includes` if compatibility is required. – jonathanGB Oct 03 '16 at 02:42
1

You can use: string.includes(".") This returns true or false

If you want a 1 or -1, simply use:

(x.includes(".")) ? 1 : -1

EDIT:

After searching a bit more, I ran across: Browser support for array.include and alternatives and others.

Use x.indexOf(".") > -1

Community
  • 1
  • 1
Cade Brown
  • 165
  • 2
  • 8
0

search interprets its parameter as a regular expression, so you need to escape the . with a (double) backslash as it will otherwise match any character:

var contains_dot = _email.search("\\.");

But because you don't need a regular expression, it would be simpler to use indexOf:

var contains_dot = _email.indexOf(".");
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 'var contains_dot = _email.search("\\.");' ...actually did the job, thanks. _email.indexOf("."); was tried before and all it returned was 0 – Really Nice Code Oct 03 '16 at 02:24
  • Hmm...there must be something else going on here as this is exactly what `indexOf` does. e.g. `'foobar'.indexOf('.')` returns -1. – JohnnyHK Oct 03 '16 at 02:34