2
function fuzzQuery(rawQuery)
{
    re = /(?=[\(\) ])|(?<=[\(\) ])/;    

    strSplit = rawQuery.split(re);

    alert(strSplit);
};

Does not work (no dialog box).

I've check the expression at http://rubular.com/ and it works as intended.

Whereas

re = /e/ 

does work.

The input string is

hello:(world one two three)

The expected result is:

hello:,(,world, ,one, ,two, ,three, )

I have looked at the following SO questions:

Javascript simple regexp doesn't work

Why this javascript regex doesn't work?

Javascript regex not working

Javascript RegEx Not Working

But I'm not making the mistakes like creating the expression as a string, or not double backslashing when it is a string.

Community
  • 1
  • 1
dwjohnston
  • 9,419
  • 24
  • 94
  • 167
  • 1
    javascript regex does not support lookbehind.. maybe an alternative here.. http://stackoverflow.com/questions/7376238/javascript-regex-look-behind-alternative – Bryan Elliott Jan 16 '14 at 05:15

2 Answers2

4

Well the main issue with your regular expression is that does not support Lookbehind.

re = /(?=[\(\) ])|(?<=[\(\) ])/
                   ^^^ A problem...

Instead, you could possibly use an alternative:

re       = /(?=[() ])|(?=[^\W])\b/;
strSplit = rawQuery.split(re);
console.log(strSplit);

// [ 'hello:', '(', 'world', ' ', 'one', ' ', 'two', ' ', 'three', ')' ]
hwnd
  • 67,942
  • 4
  • 86
  • 123
0

You can use something like this :

re = /((?=\(\) ))|((?=[\(\) ]))./;    

    strSplit = rawQuery.split(re);

    console.log(strSplit);
Sujith PS
  • 4,606
  • 3
  • 30
  • 61