0

This should pass the condition:

syntax_search = (){return 0;}
syntax_search = ( ) { fsf return 0;}
syntax_search = ( ) { return 0; }
syntax_search = (){ return; }
syntax_search = (){ if(x){ sdfsdf } return 0;}
syntax_search = (){ char x[20]; return };

It is not passing all the combinations above, What is the right way?

if( /^\s*(\s*)\s*{[\s\S]*\s+return\s*[0-9]*\s*;\s*}\s*/.test(syntax_search) )

2 Answers2

2

You regular expression contains many unneeded complexities and there are some characters that need escaping such as { and }.

Anyway you can use this modified version of your regex and it should work.

^\s*\(\s*\)\s*\{(.*(return\s*\d*\s*;)\s*)\}\s*;?$
//                                  ^
//                                  |
//                             There was a ? here

Regex 101 Demo

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • i still need that return ; or return [0-9] ; –  Aug 17 '13 at 09:24
  • my code-editor project will not support function def, so return must just be encountered for main syntax –  Aug 17 '13 at 09:30
  • it seems like [^0-9,]+ is not integrated to the post? –  Aug 17 '13 at 09:47
  • this must pass: return ; or return 0; but not: return x –  Aug 17 '13 at 09:56
  • 1
    You forgot to anchor the expression: "whatever I want here" (){ char x[20]; return }; "whatever I want here too" – Sylverdrag Aug 17 '13 at 10:04
  • 1
    @Sylverdrag I actually didn't forget. I though he can handle this bit himself. Anyway for the sake of completeness I will add them right away. Thanks. – Ibrahim Najjar Aug 17 '13 at 10:07
  • @sniffer: one last thing, return w/out ; is passing the condition, it should not –  Aug 17 '13 at 10:59
  • @sniffer: #include int main(){ return } –  Aug 17 '13 at 11:06
  • 1
    @fireflieslive Please remove previous comments so we can reduce cluttering a bit, and I have fixed the issue. All you have to do is to remove the optional meta-character `?` after the semicolon, check my modified answer please. – Ibrahim Najjar Aug 17 '13 at 11:13
0

Some issues:

  1. As M42 pointed out, you need to escape the curly brackets
  2. The parentheses at the begining also need to be escaped (otherwise you are defining a capture group)
  3. "return" is required by the expression. Your first 2 test cases don't contain the word return and will fail. Is that on purpose?
  4. Same as #3 for ;.
  5. [\s\S]* Anything which is a space and everything which isn't. Replace by a dot .* If you need to also match a newline, use [^]*
  6. This regex is not anchored to the end of the string so it will allow invalid strings. (You can put anything you want after the last }

    /^\s*(\s*)\s*{[^]return\s\d*\s*;\s*}\s*$/

Sylverdrag
  • 8,898
  • 5
  • 37
  • 54
  • 1
    Curly brackets should be escaped, but if it would be invalid (i.e., doesn't have numbers and commas only inside ) it works, e.g. `"{abc}".match(/{abc}/g)`. `/[\s\S]*/` is the same as `/.*/m`, note the m. Otherwise it doesn't match newline characters. Just notes, not criticism. – Brigand Aug 17 '13 at 09:30
  • @FakeRainBrigand No sure what's your first point. For your second, the test cases are all in one line. To match everything including a newline char, "[^]*" is a better choice in javascript. "m" does NOT make "." match newline, it causes $ to match the end of every line individually. – Sylverdrag Aug 17 '13 at 09:48
  • @FakeRainBrigand Actually, that one is consistent. Normally the flag you want is "s" for single line, but it is not supported by javascript. – Sylverdrag Aug 17 '13 at 10:08
  • Ruby's "m" makes `.` also match new lines \[[1](http://stackoverflow.com/a/5240101)]\[[2](http://www.ruby-doc.org/core-2.0/Regexp.html)]. – Brigand Aug 17 '13 at 10:14
  • Thanks. Good to know. Every language I had encountered so far used the "s" flag for that and "m" to do the opposite. – Sylverdrag Aug 17 '13 at 14:18