Using keyup , as mentioned in @experimentX's description, is how you want to go b / c, then you will know that your user has the entered value. However, executing a for loop will be extremely expensive in every keyup event. Instead, since you already know the value you need, you can use the regexp preset to search for your value:
<input type="text" id="text" value="" /> <script> $(function () { var $input = $('#text'); $input.keyup(function (e) { var regexp = /\[text\:/i, val = $(this).val(); if (regexp.test(val)) { console.log('i have it: ', val); } }); }); </script>
Here are some additional scenarios on how you can write the actual regexp .
- You want the line to be at the very beginning of the input:
var regexp = /^\[text\:/i; - Based on the above, but add any number of spaces before the text that you really want:
var regexp = /^\s+?\[text\:/i;
source share