New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something wrong with String search() method. #760

Closed
DarkOverkill opened this Issue Dec 30, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@DarkOverkill

DarkOverkill commented Dec 30, 2016

search() - can't see difference between character '.' and '_'.

str = 'foo_bar';
strPoint = 'foo.bar';
console.log('--------str test--------'); // smt wrong with first
console.log('search by point: ' + str.search('.bar')); // will return 3
console.log('indexOf by point: ' + str.indexOf('.bar')); // will return -1
console.log('search by underscore: ' + str.search('_bar')); // will return 3
console.log('indexOf by underscore: ' + str.indexOf('_bar')); // will return 3
console.log('--------strPoint test--------'); // in this case work correctly
console.log('search by point: ' + strPoint.search('.bar')); // will return 3
console.log('indexOf by point: ' + strPoint.indexOf('.bar')); // will return 3
console.log('search by underscore: ' + strPoint.search('_bar')); // will return -1
console.log('indexOf by underscore: ' + strPoint.indexOf('_bar')); // will return -1

was tested on:
Chromiun - Version 55.0.2883.87 Built on Ubuntu , running on Ubuntu 16.04 (64-bit)
Google Chrome - Version 53.0.2785.116 (64-bit)
Firefox - 50.1.0

Is this a bug? Or some feature, which I don't know about?

@shvaikalesh

This comment has been minimized.

Show comment
Hide comment
@shvaikalesh

shvaikalesh Dec 30, 2016

Contributor

String.prototype.search converts argument to a regular expression, thus dot matches any (except newline) character.

Contributor

shvaikalesh commented Dec 30, 2016

String.prototype.search converts argument to a regular expression, thus dot matches any (except newline) character.

@bakkot

This comment has been minimized.

Show comment
Hide comment
@bakkot

bakkot Dec 30, 2016

Contributor

String.prototype.search takes a regex argument, not a string (apassed string is converted to a regex), and . in a regex matches any non-newline character.

StackOverflow is probably a better place for this kind of question.

Contributor

bakkot commented Dec 30, 2016

String.prototype.search takes a regex argument, not a string (apassed string is converted to a regex), and . in a regex matches any non-newline character.

StackOverflow is probably a better place for this kind of question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment