[Javascript] RegExp for parsing search strings
Paul Novitski
paul at juniperwebcraft.com
Fri Apr 28 18:27:42 CDT 2006
At 03:19 PM 4/28/2006, Scott Reynen wrote:
>On Apr 28, 2006, at 4:22 PM, Paul Novitski wrote:
>
>>My first attempt was something like this:
>>
>> /((\"[^\"]+\")|( [^ ]+ ))+/
>>
>>one or more
>> (quotes surrounding one or more non-quote characters)
>>or
>> (spaces surrounding one or more non-space characters)
>
>The following seems to work in PHP:
>
><?
>
> $text = 'one "two three" " four " five six';
>
> preg_match_all( '#(?:"([^"]+)")|([^ ]+)#' , $text , $matches );
>
> echo( '<pre>' );
> print_r( $matches );
> echo( '</pre>' );
*Bonk* Scott, thanks for making me realize that I was barking up the
wrong tree. I was using preg_split() instead of
preg_match_all(). preg_split() finds delimiters and preg_match_all()
finds the words themselves.
Both your and my regular expressions work with preg_match_all():
http://juniperwebcraft.com/test/preg_split_Reynen01.php
http://juniperwebcraft.com/test/preg_split_Novitski01.php
but not with preg_split(). My own RegExp gives at least a partial
result with preg_split() but that's probably coincidence; I need to
start from scratch.
Regards,
Paul
More information about the Javascript
mailing list