Is it possible in JavaScript
to do something like preg_match
does in PHP
?
I would like to be able to get two numbers from string:
var text = 'price[5][68]';
into two separated variables:
var productId = 5;
var shopId = 68;
Edit:
I also use MooTools
if it would help.
JavaScript has a
RegExp
object which does what you want. TheString
object has amatch()
function that will help you out.This should work:
I found test to act more preg_match as it provides a Boolean return. However you do have to declare a RegExp var.
TIP: RegExp adds it's own / at the start and finish, so don't pass them.
match[1] and match[2] will contain the numbers you're looking for.