Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
817 views
Welcome To Ask or Share your Answers For Others

1 Answer

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...