Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] RegExp for parsing search strings


  • From: paul at juniperwebcraft.com (Paul Novitski)
  • Subject: [Javascript] RegExp for parsing search strings
  • Date: Fri Apr 28 14:40:16 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