Netizen Wys Questions: How to use only regular syntax supported by JavaScript to
Copy Code code as follows:
<p>
<table> <p> <p> </table>
<table> <p> <p> </table>
<p>
<p> between <table>...</table> is replaced by <br/>?
Thinking
One of the difficulties with this problem is that the regular nature of JavaScript support is limited. The landlord has thought of a non-JavaScript solution, as follows:
Copy Code code as follows:
re=/(<=<table.*?) (<p>) (?=.*?<\/table>)/gi;
Alert (Sourcestr.replace (Re, "<br>"));
Well, that's roughly the way it is. To be serious, even if JavaScript supports reverse-looking, the above answer does not work as intended. The reason is that the reverse-looking look (in (? <=) with Quantifiers is a more advanced syntax, and very few languages can support it. Net).
However, such regular problems as the landlord should be a very common problem, we often need to recycle some of the content. How do you answer that?
idea of a
Reading JavaScript's documentation, I found something like lastindex. According to this dongdong, I have formed this kind of thought:
• First group of large matches are found by the outer loop. The regular code is <table[^>]*>[\s\S]*?<\/table>
• Navigate to the start of this match and replace all <p> in this section of string.
• Circular execution.
I think the above ideas are generally clear, but too much detail (each match involves 3 points, a length), the solution is not leisurely, the final code must not be pleasing; it is particularly important that the whole idea is like the original crack, not the Hack of the master. And the train of thought is not related to the regular. I decided to change my way.
Idea two
The key is loops and nesting. Fortunately not the deep recursion of the inception space. Can the matching content be protected, replaced and then put back in position?
When I think of it, it's all over.
Train of thought: first find all the matching content, the track in the array inner;
At the same time using the regular, the original string split into another array wrapper;
An important feature is that wrapper must be one element more than inner, which separates inner items and is at the outermost layer. The relationship between wrapper and inner is like the relationship between 5 fingers of a palm and 4 fingers. Take the middle element out, write down the position, and then glue all the elements together after the process is finished. It's that simple. The code is as follows (in order to make the problem more general, I slightly changed the source string):
Copy Code code as follows:
<script type= "Text/javascript" >
var str= "<p> <table> <p>,<p> </table> <p> <table> <p> <p> </ table> <p> <table> <p> <p> </table> ";
var patt=/<table[^>]*>[\s\s]*?<\/table>/i;
var wrapper_result=str.split (Patt);
var inner_result = Str.match (/<table[^>]*>[\s\s]*?<\/table>/ig);
var len=inner_result.length;
var final=wrapper_result[0];
for (i=0; i<len; i++)
{
Tmp=inner_result[i].replace (/<p>/gi, "<br>");
FINAL+=TMP+WRAPPER_RESULT[I+1];
}
Alert (final);
</script>
Map:
Update
Sure enough to be capable, comments more exciting! Take a look at this code in the comments:
Copy Code code as follows:
Alert (sourcestr.replace (/<table.*?\/table>/ig, function ($) {return $1.replace (/<p>/ig, "<br>")} ));
PS: This site has just added the comments in the code parsing, you can paste code. The format is shown in the comments section of the legend. Thank you for your cooperation!