I want to check if a variable do not match this regexp:
我想检查变量是否与此正则表达式不匹配:
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
var t2 = t [2] .match(rxAfterPrint);现在我想创建一个与此模式不匹配的e varible t3
How can I do this? can you please help me?
我怎样才能做到这一点?你能帮我么?
2 个解决方案
#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?)
(承认我有一个不公平的优势,因为我知道为什么会出现这个问题:如何用JavaScript / jQuery解释textarea中的字符串?)
So my guess is you want to implement String concatenation as part of a print statement as follows:
所以我的猜测是你想要将字符串连接作为print语句的一部分实现,如下所示:
<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.
这也表明,设计一种易于程序员和编写编译器的语言非常困难。
#2
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.
我真的不清楚你的意思是“我想创建一个与这种模式不匹配的变量”。由于t2是你的匹配,似乎你想让t3成为不匹配的对象。
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.
另一种方法是使用replace(),但我相信match()是更好的选择。
output2.value = input.value.replace(/(^ *\+ *("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)$\s*)+/gm,"")
For both cases, I added the g
lobal and m
ultiline 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.
对于这两种情况,我添加了全局和多线,以便一次轻松测试多条线。如果你只是测试一个,那么同时删除g和m,否则它可能会错误地告诉你一个字符串传递或者当它没有传递时会导致错误。
Demo: JSFiddle
#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?)
(承认我有一个不公平的优势,因为我知道为什么会出现这个问题:如何用JavaScript / jQuery解释textarea中的字符串?)
So my guess is you want to implement String concatenation as part of a print statement as follows:
所以我的猜测是你想要将字符串连接作为print语句的一部分实现,如下所示:
<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.
这也表明,设计一种易于程序员和编写编译器的语言非常困难。
#2
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.
我真的不清楚你的意思是“我想创建一个与这种模式不匹配的变量”。由于t2是你的匹配,似乎你想让t3成为不匹配的对象。
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.
另一种方法是使用replace(),但我相信match()是更好的选择。
output2.value = input.value.replace(/(^ *\+ *("(?:[^"]*)"|(?:[a-zA-Z]\w*)) *(.*)$\s*)+/gm,"")
For both cases, I added the g
lobal and m
ultiline 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.
对于这两种情况,我添加了全局和多线,以便一次轻松测试多条线。如果你只是测试一个,那么同时删除g和m,否则它可能会错误地告诉你一个字符串传递或者当它没有传递时会导致错误。
Demo: JSFiddle