JavaScript中的preg_match? [英] preg_match in JavaScript?

查看:174
本文介绍了JavaScript中的preg_match?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript 是否可以在 PHP中执行类似 preg_match 的操作

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

分成两个变量:

var productId = 5;
var shopId    = 68;

编辑:
我还使用 MooTools 如果有帮助的话。

I also use MooTools if it would help.

推荐答案

JavaScript有 RegExp 对象,它可以满足您的需求。 String 对象有 match() 可以帮助你的功能。

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+)\]/);

这篇关于JavaScript中的preg_match?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆