Tuesday, 15 June 2010

regex - Regular expression doesn't work -



regex - Regular expression doesn't work -

i have simple line of code using regular expressions want substitute pieces of strings empty space:

newaddress = myaddress.replace(/^.*?(ramp|arterial|majorroad|street &|highway &|highway|street|street &|street & highway|arterial & street|street & arterial|majorroad &|majorroad & ramp|ramp & majorroad|major road|highway & majorroad)\,/gi, '');

but having in variable this:

highway & contrada torremuzza, 95121 catania ct

why didn't removed "highway &" part?

it looks me need neither .* nor comma. .* cause replace precedes string.

try this:

(ramp|arterial|majorroad|street &|highway &|highway|street|street &|street & highway|arterial & street|street & arterial|majorroad &|majorroad & ramp|ramp & majorroad|major road|highway & majorroad)

or, if you're in mood fancy optimizations:

(?:majorroad & )?ramp|(?:major r|(?:(?:ramp|highway) & )?majorr)oad|(?:highway|majorroad|street) &|(?:arterial & )?street|(?:street & )?(?:arterial|highway)

just kidding. in theory more efficient, it's harder maintain.

regex

No comments:

Post a Comment