|

regex – preg_match in JavaScript?

regex – preg_match in JavaScript?

JavaScript has a RegExp object which does what you want. The String object has a match() function that will help you out.

var matches = text.match(/price[(d+)][(d+)]/);
var productId = matches[1];
var shopId    = matches[2];
var text = price[5][68];
var regex = /price[(d+)][(d+)]/gi;
match = regex.exec(text);

match[1] and match[2] will contain the numbers youre looking for.

regex – preg_match in JavaScript?

var thisRegex = new RegExp([(d+)][(d+)]);

if(!thisRegex.test(text)){
    alert(fail);
}

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 its own / at the start and finish, so dont pass them.

Leave a Reply

Your email address will not be published. Required fields are marked *