user3044394 user3044394 - 2 months ago 11
Javascript Question

find multiple digits and a colon within a string using regex

I'm trying to find any string of digits followed by a colon.
Here is an example string:

var str = " 234: all kinds of code";


Here is what I tried:

str.search(/^\d+:$/);


and that returns a -1 so it is not finding the digits followed by the colon.

I tried this and it returned 0:

/^.+\d+:.+$/

Answer

Your regex is only successful when the string to search begins and ends with digits followed by colon. Try this:

str.search(/\d+:/);