Question

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.

Was it helpful?

Solution

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', ')' ]

OTHER TIPS

You can use something like this :

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

    strSplit = rawQuery.split(re);

    console.log(strSplit);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top