Jamie Bicknell Jamie Bicknell - 1 year ago 75
PHP Question

Porting regex from PHP to JavaScript

I have the following regex in PHP:


and I would like to port it to javascript like:

var regex = new RegExp('/(?<=\')[^\'\s][^\']*+(?=\')|(?<=")[^"\s][^"]*+(?=")|[^\'",\s]+/');

var match = regex.exec("hello,my,name,is,'mr jim'")

for( var z in match) alert(match[z]);

There is something that JavaScript doesnt like here, but I have no idea what it is. I've tried looking for diferences between PHP and JS regex via regular-expressions.info but I cant see anything obvious.

Any help would be greatly appreciated

Thank you again

The problem seems to lie within the positive lookbehind's but does this mean it cannot be ported?


You don't get lookbehind (and lookahead has problems in IE, so is best avoided too). But it's easy to just let those ' and " characters be part of the match, and throw them out afterwards:

var value= "hello,my,name,is,'mr jim'";
var match;
var r= /'[^'\s][^']*'|"[^"\s][^"]*"|[^'",\s]+/g;

while(match= r.exec(value)) {
    var text= match[0];
    if ('"\''.indexOf(text.charAt(0))!=-1) // starts with ' or "?
        text= text.substring(1, text.length-1);

Or, use capturing parentheses to isolate the quotes from the text:

var r= /'([^'\s][^']*)'|"([^"\s][^"]*)"|([^'",\s]+)/g;

while (match= r.exec(value)) {
    var text= match[1] || match[2] || match[3];

(I'm guessing your for(var z in match) was supposed to loop over each pattern match in the string. Unfortunately JavaScript doesn't quite work that easily.)

This may not be the best way to parse a comma-separated list; it seems a bit ill-defined for cases where you have a space or quote in the middle of a field. A simple string-indexing parser might be a better bet.