So, I'm building a simple compiler for HTML in javascript. I'm using the match() expression to match strings and tags. In trying to verify the expression:
我正在用javascript为HTML构建一个简单的编译器。我使用match()表达式来匹配字符串和标记。试图验证这个表达式:
<p>Paragraph</p>
I'm using:
我用的是:
var = str.match(/<p>(.*?)Paragraph/g);
However, it doesn't recognize the code, if in the html field, I type:
但是,它不识别代码,如果在html字段中,我输入:
<p>
Paragraph</p>
So, I searched and found that if I use the following, it fixes the issue with the next line:
所以,我搜索后发现,如果我使用以下语句,它会在下一行中修复问题:
var = str.match(/<p>(.*?(\n))+.*?Paragraph/g);
But then if I revert to my first html code the problem persists. Is there a way that both of these situations can be handled? (while using the match function of course)
但是如果我回到我的第一个html代码,问题就会继续存在。有没有一种方法可以同时处理这两种情况?(当然是使用match函数)
So it doesn't make a difference whether I type <p>Paragraph</p>
or
所以我输入
段落
或。<p>
Paragraph</p>
1 个解决方案
#1
3
In this part
在本部分中
(.*?(\n))+
change +
to *
. +
means "any number of newlines 1 or more times". You want "0 or more times".
改变+ *。+表示“任意数量的新行1或更多次”。你想要“0或更多次”。
So fixed expression:
所以固定表达式:
/<p>(.*?(\n))*.*?Paragraph/g
#1
3
In this part
在本部分中
(.*?(\n))+
change +
to *
. +
means "any number of newlines 1 or more times". You want "0 or more times".
改变+ *。+表示“任意数量的新行1或更多次”。你想要“0或更多次”。
So fixed expression:
所以固定表达式:
/<p>(.*?(\n))*.*?Paragraph/g