0

need help with regex

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

need to get all params (name -> value)

tried this way /(\w+)\s?=(\S+)[\s|,]/

result:

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,"
  }
}
Subdigger
  • 2,166
  • 3
  • 20
  • 42

3 Answers3

0

Try this:

/(\w+)\s*=\s*([^,\s]*)/
poncha
  • 7,726
  • 2
  • 34
  • 38
0

Try this:

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

I'm assuming that both parameter and value may contain only letters and digits. You may choose to be more permissive by writing this:

([^=\s,]+)\s*=\s*([^=\s,]+)
davidrac
  • 10,723
  • 3
  • 39
  • 71
0

I tried to use the split method, but don't know if it's good. Comments are welcome.

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

Result (arg_array):

({left:"123", top:"345", width:"123", height:"34", center:"yes", help:"no", resizable:"yes", status:"now"})
eminor
  • 923
  • 6
  • 11