[Javascript] RegExp for parsing search strings

Triche Osborne wdlists at triche-osborne.com
Fri Apr 28 15:54:14 CDT 2006


Paul Novitski wrote:
> Given a typical search string potentially consisting of any number of 
> single terms and quoted phrases, such as these examples:
> 
>     item1
>     item1 item2
>     "phrase one"
>     "phrase one" "phrase two"
>     "phrase one" item2 "phrase three" item4
> 
> design a regular expression that splits it into its components, e.g.:
> 
> source:
>     item1 "phrase two" item3 item4 "phrase five 5" "phrase six" item7
> result:
>     item1
>     phrase two
>     item3
>     item4
>     phrase five 5
>     phrase six
>     item7
> 

Okay, I'm going to ask a question which I'm sure has a reasonable 
answer, but I can't help being curious: Why hasn't the source been 
impregnated with a delimiter other than a space? This would make it a 
simple matter of exploding (PHP) or splitting (JS) the string, which 
avoids the drag that regex imposes on optimization in PHP.

Triche




More information about the Javascript mailing list