javascript - Detecting if period exists in a string -


i apologize if seems dumb question, how go searching string see if contains period? i've tried searching:

var p="this text without period, now?"  alert(p.search(".")); 

i under impression should return -1 because there no period in sentence. however, returns 0.

am missing something?

there's many ways this, use indexof() see if character exists in string :

alert( p.indexof(".") != -1 ); // true or false 

according mdn, search() "executes search match between regular expression , string object.", :

alert( p.search(/\./) ); 

which give -1, , period has special meaning in regex, , must escaped.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -