[Javascript] RegExp for parsing search strings

Scott Reynen scott at randomchaos.com
Fri Apr 28 17:19:30 CDT 2006


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>' );

?>

Output:

-----
Array
(
     [0] => Array
         (
             [0] => one
             [1] => "two three"
             [2] => " four "
             [3] => five
             [4] => six
         )

     [1] => Array
         (
             [0] =>
             [1] => two three
             [2] =>  four
             [3] =>
             [4] =>
         )

     [2] => Array
         (
             [0] => one
             [1] =>
             [2] =>
             [3] => five
             [4] => six
         )

)
-----

But I suspect there's some unconsidered case that won't cover.  And  
I'm not sure how to translate it into JavaScript.

Peace,
Scott



More information about the Javascript mailing list