my code stock.com

Catherine Stevens

match on first instance of letter range
by Catherine Stevens

will return 5 cuz 5 is the character # of the first instance of a letter in t-z. it uses the ASCII codes so uppercase A-Z goes first and then lowercase a-z.

adding an * after the brackets indicates looking for any number of those letters.

adding a number in {} after the brackets indicates that you want to find that number of characters in a row

adding a \b on either end indicates looking for it on a word boundary, which means

Snippet options

Download: Download snippet as match-on-first-instance-of-letter-range.js.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

var myString = "The brownbrown brown fox jumps";
var offset = myString.search(/The/);
alert(offset);          // 0


var myString = "The quick brown fox jumps over the lazy dog";
var offset = myString.search(/[t-z]/);
alert(offset);          // 5


var myString = "The brownbrown brown fox jumps";
var offset = myString.search(/\b[a-z]{5}\b/);
alert(offset);          // 15


var myString = "my zip is 01720-1234 what is yours?";
var offset = myString.search(/\b[0-9]{5}(?:-[0-9]{4})?\b/);
alert(offset);          // 10

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.