Javascript String Matching Only At The Start Versus Using Indexof?
I currently am matching user input as follows: user_input = 'upload a file' if ( 'upload a file'.indexOf(user_input.toLowerCase()) > -1 ) {} This work fine but the problem is,
Solution 1:
indexOf
return the index of the match, so if you want to test if it match at the beginning just check if it returns 0
user_input = "upload a file"
if ( "upload a file".indexOf(user_input.toLowerCase()) == 0 ) {}
Solution 2:
What you describe means you want compare with zero:
if ( "upload a file".indexOf(user_input.toLowerCase()) == 0) { }
Solution 3:
<script>
user_input = "upload a file"if ( "upload a file".**substr**(0, user_input.length) == user_input.toLowerCase()) {}
</script>
Use the inputs to your advantage...
http://www.w3schools.com/jsref/jsref_substr.asp
Grab first X characters of the string, and make sure the whole string matches or any number of the characters you would like.
Post a Comment for "Javascript String Matching Only At The Start Versus Using Indexof?"