views:

463

answers:

6

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
}
+6  A: 

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
VoteyDisciple
+1 but the parens and the + could be more than required, depending on what exactly the OP wants - I'd explain the meaning for him.
annakata
+2  A: 
if( it.match(/^string:\d+$/) ( {
   ...
}
Alex Martelli
+1  A: 

The above is good for integer numbers; if you want floating point numbers, or even scientific notation (as understood in C-like languages), you'll want something like this:

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

You can remove the first [+-]? if you don't care about sign, the (.[0-9]+)? if you don't care about floating points, and the ([eE][+-]?[0-9]+)? if you don't care about scientific notation exponents. But if there's a chance you DO want to match those, you want to include them as optional in the regex.

Platinum Azure
My answer does NOT force the string to match the full rules of scientific notation, which entails exactly one digit left of the decimal point. If you want that, you'll want something like([+-]?[0-9]+(\.[0-9]+)?|[+-]?[0-9](\.[0-9]+)?([eE][+-]?[0-9]+)?)The first part, left of the "|" character, requires an optional sign, 1+ digits, and optionally a decimal w/more digits. The second part requires a valid scientific notation number, including only one digit left of the decimal.You can change the "+" after the digits in decimal point to "*" if you want to allow for numbers like "5." as well.
Platinum Azure
+1  A: 
if(teststring.match(new RegExp("^" + knownstring + ":\d+$"))) {
  // some code
}
slosd
A: 
if(!!"string:5456".match(/^string:\d+$/)) { ... }

Number is a integer in the example above.

Philipe Fatio
Why would you put that double negation at the beginning?
VoteyDisciple
To try and coerce a boolean out of it. You're better off using .test() here
Sean Bright
+1  A: 

If you want only to check if the input string matches the pattern, you can use the RegExp.test function:

if (/^string:[0-9]+$/.test(input)){
  //..
}

or with the String.search function:

if (input.search(/^string:[0-9]+$/) != -1){
  //..
}

If you want to validate and get the number:

var match = input.match(/^string:([0-9]+)$/),
    number;

if (match){
  number = +match[1]; // unary plus to convert to number
  // work with it
}
CMS