[Javascript] RegExp for parsing search strings

Paul Novitski paul at juniperwebcraft.com
Fri Apr 28 14:33:15 CDT 2006


Someone posted this problem to another list I belong to, and after a 
half hour of fruitless experimenting I throw it open to the RegExp 
geniuses on this list:

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

The problem was posed in the context of PHP which has the function 
preg_split <http://php.net/preg_split> but I imagine the same reg exp 
logic could be used with the match function in both PHP & JavaScript.

Any takers?

Paul




More information about the Javascript mailing list