0

需要正则表达式的帮助

var s = "left=123,top=345,width=123, height= 34, center=yes, help=no,resizable=yes, status=now";

需要获取所有参数(名称 -> 值)

试过这种方式/(\w+)\s?=(\S+)[\s|,]/

结果:

array(3) {
  [0] =>
  array(3) {
    [0] =>
    string(28) "left=123,top=345,width=123, "
    [1] =>
    string(12) "center=yes, "
    [2] =>
    string(23) "help=no,resizable=yes, "
  }
  [1] =>
  array(3) {
    [0] =>
    string(4) "left"
    [1] =>
    string(6) "center"
    [2] =>
    string(4) "help"
  }
  [2] =>
  array(3) {
    [0] =>
    string(22) "123,top=345,width=123,"
    [1] =>
    string(4) "yes,"
    [2] =>
    string(17) "no,resizable=yes,"
  }
}
4

3 回答 3

0

试试这个:

/(\w+)\s*=\s*([^,\s]*)/
于 2012-08-14T09:14:21.550 回答
0

试试这个:

([\w\d]+)\s*=\s*([\w\d]+)

我假设参数和值都可能只包含字母和数字。您可以通过编写以下内容来选择更加宽容:

([^=\s,]+)\s*=\s*([^=\s,]+)
于 2012-08-14T09:15:16.693 回答
0

我尝试使用拆分方法,但不知道它是否好。欢迎评论。

var s = "left=123,top=345,width=123, height= 34, center=yes, help=no,resizable=yes, status=now";
var params = s.replace(/\s*,\s*/g,',').replace(/\s*=\s*/g,'=').split(',');

var arg_array = {};

for(var n = 0, len=params.length; n < len; n++) {
    var arg = params[n].split("=");
    arg_array[arg[0]] = arg[1];
}

结果(arg_array):

({left:"123", top:"345", width:"123", height:"34", center:"yes", help:"no", resizable:"yes", status:"now"})
于 2012-08-14T10:44:25.217 回答