[英]regular expression to compare variables inside html tags
Ive been going through the regular expression things and consider we have an array which has html tags stored in as strings.. like array=["</div>,"</a>", "<div id='test'>", "<a href='http://test/new.html'>", "</a>", "</div>", "<span style='color:#ffffff;'>"]
我一直在研究正则表达式的东西,并考虑我们有一个数组,其中的html标签存储为字符串。.如
array=["</div>,"</a>", "<div id='test'>", "<a href='http://test/new.html'>", "</a>", "</div>", "<span style='color:#ffffff;'>"]
if we go through the array one by one, 如果我们一个接一个地遍历数组,
if we want to detect complete tags and incomplete tags, like for example, <div>
is openened and </div>
its closed in location after that.. so those two comes under complete tags... <span>
is opened, but never closed, so it comes under incomplete open tags, </div>
at first location is closed, so it comes under incomplete closed tags. 如果我们要检测完整标签和不完整标签,例如
<div>
被打开并且</div>
在此之后被关闭的位置..因此,这两个都位于完整标签下... <span>
被打开,但是永远不会关闭,因此它位于不完整的打开标记下, </div>
首先处于关闭状态,因此它位于不完整的关闭标记下。
Can this be done using JavaScript? 可以使用JavaScript完成吗?
This sounds like a great problem for teaching the utilities of the Stack data type. 对于教导Stack数据类型的实用程序来说,这听起来像是一个很大的问题。 In Javascript, all arrays have use of stack functions, as seen here .
在Javascript中,所有阵列都使用堆栈功能,看到这里 。
How do you do it? 你怎么做呢?
Start with an empty stack. 从一个空堆栈开始。 Loop through the elements of your array, in order.
按顺序循环遍历数组的元素。 For each item:
对于每个项目:
Finally, when you've finished your array, check that the stack is empty. 最后,完成阵列后,检查堆栈是否为空。
For example: 例如:
[<div>, <a>, <span>, </span>, </div>, </a>]
Will fail. 将失败。 Push
div
, push a
, push span
. 推
div
,推a
,推span
。 Pop span
. 流行
span
。 Pop a
, which does not match div
弹出
a
与div
不匹配的,
It should look like 它看起来像
[<div>, <a>, <span>, </span>, </a>, </div>]
Which will pass, as the stack will be empty (have a length of 0) at the end of the method. 该方法将通过,因为在方法结束时堆栈将为空(长度为0)。
Edit: If you want to use regex in the separate steps: 编辑:如果要在单独的步骤中使用正则表达式:
To determine if a tag is open: 要确定标签是否打开:
tag.match(/<\//) == null
This checks if a tag contains the characters <\\
这将检查标签是否包含字符
<\\
To get the content from a tag: 要从标签获取内容:
var tagContent = tag.match(/\w+/)[0];
This grabs a group of word characters. 这将抓取一组文字字符。 Specifically, the first group.
具体来说,第一组。 It should just grab the tag name and ignore any attributes, as the whitespace will end the expression.
它应该只获取标记名,而忽略任何属性,因为空格将结束表达式。 Match always returns an array, in case you matched globally (where there would be multiple matches), so get the 0-th index of the returned array to get the value.
如果您全局匹配(其中将有多个匹配项),则Match始终返回一个数组,因此获取返回数组的第0个索引以获取值。
To compare two tags: 比较两个标签:
tag1Content == tag2Content
This seriously does not require regex. 这严重不需要正则表达式。
var stack=new Array();
var stackCount = 0;
var array=["</div>","<div id='test'>", "<a href='http://test/new.html'>", "</a>", "</div>", "<span style='color:#ffffff;'>"];
for(var k=0;k<array.length;k++)
{
alert("Eleemnt Selected:" + array[k]);
if(stackCount==0)
{
if(array[k].match(/<\//)==null)
{
stack.push(array[k]);
stackCount++;
alert("Pushed in to the stack as stack is empty" + stack);
}
else
{
alert("Ignored as it is a tag containing slash");
}
}
else
{
var tagType1=array[k].match(/<\//);
var tagType2=stack[0].match(/<\//);
var cmpTag1=array[k].match(/\w+/);
var cmpTag2=stack[0].match(/\w+/);
//alert("Tag Type : Array element :" +tagType1 + "Stack element : " + tagType2 + "Tag Name: Array element : " + cmpTag1 + "Stack Element : " +cmpTag2);
if(cmpTag1!=cmpTag2 && tagType1!=tagType2)
{
stack.pop();
stackCount--;
alert("same Tags, so popped out from the stack" + stack);
}
else
{
stack.push(array[k]);
stackCount++;
alert("Pushed in to the stack as items compared are different");
}
}
}
Ive posted my answer...I tried and this is where I am... any feedbacks or improvemnts from your side??? Ive发布了我的答案...我已经尝试过了,这就是我的位置...您身边的任何反馈或改进之处??? I m concerned about the part, , since it is a closed tag, it should pop out.. right??
我担心的部分是,因为它是封闭标签,所以应该弹出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.