0

I want to check if a variable do not match this regexp:

DEMO

So this is the pattern that match the regexp in my code:

rxAfterPrint = new RegExp(/^ *\+ *("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)$/);

and in this way I check for matching:

var t2 = t[2].match(rxAfterPrint); and now I want to create e varible t3 that dont match this pattern

How can I do this? can you please help me?

orsina
  • 131
  • 11
  • @PaulS. I believe that in this [match](http://www.w3schools.com/jsref/jsref_match.asp) case is returning an array to be assigned to t2. – drs9222 May 03 '15 at 01:25
  • yes the answer of @PaulS. does not function. The variable t3 doesn not have value – orsina May 03 '15 at 01:27
  • I suspect you want to use functions on the [regex object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) itself to get a little more flexibility. – drs9222 May 03 '15 at 01:27
  • @drs9222 if there was no match, `t2` is `null` and `!null` is `true`, which is what I thought OP wanted. What is the expected value of `t3`? – Paul S. May 03 '15 at 01:29
  • @PaulS. The `match` function on a string will return an array when given a regular expression. – drs9222 May 03 '15 at 01:32
  • @drs9222 `.match` on a _String_ that fails `'foo'.match(/bar/); // null` gives `null`. Further, a _RegExp_ does not have a `.match` method, `/bar/.match; // undefined` – Paul S. May 03 '15 at 01:36
  • @PaulS. You're spot on. I knew that there were functions on regex for testing and I assumed (without verifying) that was what you were referring to. However if it matches then `t2` will be an array and `!t2` will not be an array containing non-matches. – drs9222 May 03 '15 at 01:39
  • @drs9222 So you're saying OP was looking for `var t3 = t2 ? null : [t[2]];` ? – Paul S. May 03 '15 at 01:41
  • @PaulS. It does seems that the crux of my confusion is that we think the OP is looking for different things and I was incorrectly assuming that you were trying to give a answer that would result in what I thought the OP was looking for. I thought the OP wanted the parts that don't match not just a value saying they don't match. That said, you're probably more correct because I'm clearly not thinking right tonight. – drs9222 May 03 '15 at 01:47

2 Answers2

1

(Admitting I have an unfair advantage because I knew why this problem did arise: How can I interpret strings in textarea with JavaScript/jQuery?)

So my guess is you want to implement String concatenation as part of a print statement as follows:

<string> ::= '"' <character>* '"' | <variable>
<print> ::= 'print' <string> ('+' <string>)*
<print> ::= 'print' (<string> '+')* <string>

The two <print> actually express the same, using the 2nd version you can first (after matching /^ *print */) try to apply the pattern rxConcat as many times a possible and if this doesn't match, then you apply the 2nd expression rxStringValEOL to match the remainder (if no success, it's an invalid statement):

rxConcat = new RegExp(/ *(?:"([^"]*)"|([a-zA-Z]\w*)) *\+ */);
rxStringValEOL = new RegExp(/ *(?:"([^"]*)"|([a-zA-Z]\w*)) *$/);

This also shows that it is pretty difficult to design a language that is easy for the programmers and for those who write the compilers.

Community
  • 1
  • 1
maraca
  • 8,468
  • 3
  • 23
  • 45
  • Thanks @maraca. I solve it now. I use `rxErrorPrint = new RegExp(/^(\s*\+\s*(?:"[^"]+"|[a-zA-Z]\w*)\s*)+$/);` and if the text that matches this is null(means that the print statements is incorrect) than I print an error message. Anyway I will also see your answer. Thanks for your support:) – orsina May 03 '15 at 21:35
  • @orsina you are welcome, if you understand the difference between the two print in the BNF then you understood the most important. You will see that the 2nd version will be much easier if you try to implement it and you can directly say where the error occured. – maraca May 03 '15 at 21:42
  • Thanks @maraca. I will see your version now . I have one more question please? I see that you always ilustrate examples with BNF grammar. In fact I have defined the syntax rules for my language, but I have not built the BNF grammar. Do you recommend me to build it? And could you suggest me any starting tutorial for this? I have done some searches on google cocncerning BNF, but I have not start to build one. – orsina May 03 '15 at 21:50
  • @orsina The wikipedia article about BNF is not too bad. Also note that often there are little adaptations (like the * here, what I used here is a little closer to regex), it is just important that you are consistent when defining the grammar. And yes it surely helps to build it. Then try to eliminate tail recursion. You probably could write books about that, I'm no expert in language design. And I recommend reading this: http://stackoverflow.com/questions/33923/what-is-tail-recursion – maraca May 03 '15 at 21:57
  • @orsina I might have used the term tail-recursion wrong here , better read some articles and don't take my word for it, however the rest should be correct. – maraca May 03 '15 at 22:02
  • thanks for both suggestion. I will read and work on it.thanks:) – orsina May 03 '15 at 22:30
0

It's really unclear what you mean by "I want to create a variable that don't match this pattern". Since t2 is your match, it seems like you want t3 to be objects that don't match.

Because you're anchoring to the start of the string (^), this is a really great place to use a negative lookahead with almost the identical regex. Literally, all I did was surround it with (?! and ) and .* at the end..

output1.value = input.value.match(/^(?! *\+ *("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)).*$/gm).join("\r\n")

An alternative is to use replace() like so, but I would believe match() is the better option.

output2.value = input.value.replace(/(^ *\+ *("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)$\s*)+/gm,"")

For both cases, I added the global and multiline to easily test several lines at once. If you're only testing one, remove both the g and the m, otherwise it could cause bugs by incorrectly telling you a string passed or failed when it didn't.

Demo: JSFiddle

Regular Jo
  • 5,190
  • 3
  • 25
  • 47