regex - String.prototype.match differences between Chrome/Safari & FireFox -


i have pretty simple regex expression,

"lang-bash".match("lang-([a-z]+)", "gi") 

and on latest chrome (43-ish) expected behaviour of ["lang-bash", "bash"]. when try same on latest firefox (39 or 41a) ["lang-bash"]. tried in safari (8.0.7) , has expected result of ["lang-bash", "bash"].

so might bug in firefox, can submit for, i'm expecting it's don't understand. thanks!

edit: alright... more testing.

firefox

"lang-bash lang-dash".match(/lang-([a-z]+)/i) array [ "lang-bash", "bash" ] "lang-bash lang-dash".match(/lang-([a-z]+)/ig) array [ "lang-bash", "lang-dash" ] "lang-bash lang-dash".match("lang-([a-z]+)", "gi") array [ "lang-bash", "lang-dash" ] 

safari

"lang-bash lang-dash".match(/lang-([a-z]+)/i) [ "lang-bash", "bash" ] "lang-bash lang-dash".match(/lang-([a-z]+)/ig) [ "lang-bash", "lang-dash" ] "lang-bash lang-dash".match("lang-([a-z]+)", "gi") ["lang-bash", "bash"] 

chrome

"lang-bash".match(/lang\-([a-z]+)/i) ["lang-bash", "bash"] "lang-bash".match(/lang\-([a-z]+)/gi) ["lang-bash"] "lang-bash lang-dash".match("lang-([a-z]+)", "gi") ["lang-bash", "bash"] 

since there's capture, i'd expect result of flags ig [ "lang-bash", "lang-dash", "bash", "dash" ] guess that's not correct @ all. seems browsers act differently here. have idea what's going on?

originally, firefox implements non-standard flags argument in string.prototype.{search, match, replace}. argument deprecated in firefox 39 , removed in future version. see more in bug report issue.

what see in firefox due effect of flags argument.

when match used global g pattern, main matches included. without global flag, match returns first match in index 0 , captured text in indices 1.

since firefox aware of flags argument, returns main matches without capturing groups, seen in:

"lang-bash lang-dash".match("lang-([a-z]+)", "gi") "lang-bash".match("lang-([a-z]+)", "gi") 

other browsers ignore flags argument, result shows first match , capturing groups.

if need flags, specify in regexp literal (if pattern fixed) or regexp constructor (if pattern dynamic).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -