Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "?-mix:" mean in regex

What does "?-mix:" mean in regex, also is this valid in javascript/jQuery? If it's not valid, what is an appropriate substitute.

Update: This is the full regex /(?-mix:^[^,;]+$)/

Its used in javascript in chrome, and I'm getting the following error:

Uncaught SyntaxError: Invalid regular expression: /(?-mix:^[^,;]+$)/: Invalid group

Note: I found this helpful: How to translate ruby regex to javascript? - (?i-mx:..) and Rails 3.0.3

like image 283
user160917 Avatar asked Apr 26 '13 14:04

user160917


2 Answers

Assuming perl context, (?-mix) this would

  • -m disable multiline matching
  • -i disable case insensitive matching
  • -x disable extended regex whitespace

See here: http://www.regular-expressions.info/modifiers.html

Not all regex flavors support this. JavaScript and Python apply all mode modifiers to the entire regular expression. They don't support the (?-ismx) syntax, since turning off an option is pointless when mode modifiers apply to the whole regular expressions. All options are off by default.

like image 163
sehe Avatar answered Oct 27 '22 18:10

sehe


Since you've now made it clear that the context here is javascript, I don't think javascript supports that flag syntax so it's complaining about ^ being found in the middle of a regex. Since you can never find the start of a match in the middle of a match, this makes an invalid regex.

In javascript, you specify flags outside the regex itself such as:

var re = /^[^,;]+$/mi;

or as an argument like this:

var re = new RegExp("^[^,;]+$", "mi");

The x flag is not supported in javascript and this particular regex does not need the i flag.

What this particular regex is trying to do is to tell you whether the string you are matching it against contains all characters other than , and ; and is not empty.

like image 36
jfriend00 Avatar answered Oct 27 '22 20:10

jfriend00