[Javascript] RegExp for parsing search strings

Steve Clay sclay at ufl.edu
Fri Apr 28 20:00:43 CDT 2006


Friday, April 28, 2006, 7:27:42 PM, Paul wrote:
> preg_split() finds delimiters and preg_match_all()
> finds the words themselves.

preg_split() called with the flags as I sent will also return the
captures, I just forgot to capture the expression in the e-mail I set you
(that's what I get for throwing it together before running out the door at
5 on Friday).

Anyway, you wanted this in JS? This will handle single or double quotes and
allow the opposite inside.

var
  test = ' apple "\'banana crust" dand\'elion"eleph\'ant " fawn grass " fandango',
  m = test.match(/"[^"]+"|'[^']+'|[^\s'"]+/g);
for (var i=0, l=m.length; i<l; i++) {
  m[i] = m[i].replace(/^['"]|['"]$/g, '');
}
alert(m);

Steve
-- 
http://mrclay.org/




More information about the Javascript mailing list