Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
41
rated 0 times [  44] [ 3]  / answers: 1 / hits: 58376  / 15 Years ago, tue, august 11, 2009, 12:00:00

I have a string of the format: string:num where num is any number but string is a known string that I need to match on. I'd like to have this in an if statement as:



if( it matches 'string:' followed by a number) {
//do something
}

More From » jquery

 Answers
8

You want ...



if (stringYouHave.match(/^string:([0-9]+)$/)) {
// do something
}


This includes:




  1. ^ beginning of the string

  2. string: the literal string: you mentioned

  3. (.....) This subexpression, which you can refer to later if you need to know which number is in the string (though in this particular case, you could also just replace 'string:' with '')

  4. [0-9] a character between 0 and 9 (i.e., a digit)

  5. + Must have at least one of those (i.e., digits mentioned above), but can have any number

  6. $ end of the string


[#98934] Friday, August 7, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;