Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

case insensitive indexOf ? 1

Status
Not open for further replies.

stranger123

Programmer
Apr 16, 2006
76
GB
Hi,

Want to test "abc" in s or not with case insensitive?
s.indexOf("abc")==-1 is case sensitive.


 
Do it like this.
[tt]
//case sensitive use of indexOf
if (s.indexOf("abc")==-1) {
//do something when not found
}
//case insensitive expansion
if (!/abc/i.test(s)) {
//do something when not found
}
[/tt]
 
Thank you.
I am new in javascript....what is the function test() for?
 
>what is the function test() for?
[tt]
/abc/i : regular expression (/abc/) with case ignored flag (i)
[/tt]
msdocumentation said:
test Method
Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

rgExp.test(str)
Arguments
rgExp
Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags.
str
Required. The string on which to perform the search.
Remarks
The test method checks to see if a pattern exists within a string and returns true if so, and false otherwise.
 
Thank you.

So, test() is a method of the regular expression object? It is not a pure javascript function?
And, can we use "." insige the regular expression something like
/.txt/i.test(s) ?
 
>So, test() is a method of the regular expression object? It is not a pure javascript function?

Pure?!

>And, can we use "." insige the regular expression something like /.txt/i.test(s) ?

If "." is a literal dot, it is this.
[tt] /\.txt/i.test(s)[/tt]
 
Thank you.

If test() function can be used only after a regular expression ?
I have just tested that /.txt/i.test(s) was working fine??
 
>I have just tested that /.txt/i.test(s) was working fine??
I've given you the answer. Why the question? Why don't you read the answer.

>If test() function can be used only after a regular expression ?
Just like .indexOf() is a "function" used only after a string object/string literal. But it has no hegemony over its name, just like indexOf has none.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top