Cleverly solve nested replacement in JavaScript (strong regular expression)

Source: Internet
Author: User

Wys: how to use only the regular syntax supported by JavaScript

Copy codeThe Code is as follows: <p>
<Table> <p> </table>
<Table> <p> </table>
<P>

<P> between <table>... </table> is replaced with <br/>?
Thoughts
One of the difficulties of this problem lies in the limited regular expression features supported by JavaScript. The author has come up with a non-JavaScript solution, as follows:Copy codeThe Code is as follows: re = /(? <= <Table .*?) (<P> )(? = .*? <\/Table>)/gi;
Alert (sourcestr. replace (re, "<br> "));

Well, the idea is roughly like this. To be honest, even if JavaScript supports reverse view, the above answer cannot be run as expected. The reason is that backward view with quantifiers (that is, in (? <=) Used in ?, *, +, {} Is a more advanced syntax, with very few languages supported (except. Net ).

However, regular expressions like the landlord should be a simple problem. We often need to replace some content cyclically. How can I solve this problem?

Train of Thought 1

After reading the JavaScript documentation, I found something like lastIndex. According to this idea, I have formed the following idea:
• First, press the outer loop to find the large match of the first group. The regular code is <table [^>] *> [\ s \ S] *? <\/Table>
• Locate the starting position of the end of the match and replace all <p> in the string.
• Loop execution.
I think the above ideas are roughly clear, but there are too many details (each matching involves three locations and one length). It is not easy to solve, and the final code will not be pleasing to the eye; it is particularly important that the entire idea is like the original Crack, rather than the master's Hack. In addition, the idea does not have much to do with regular expressions. I decided to change the path.

Train of Thought 2

The key is loop and nesting. Fortunately, it is not the deep recursion of the dreamspace. Can I protect the Matching content and put it back in place after replacement?

When I think of this, I am very enlightened.

Train of Thought: First find all the Matching content and record it in the array inner;

Use this regular expression to split the original string into another array wrapper;

An important feature is that wrapper must have one more element than inner, which separates inner items one by one and is in the outermost layer. The relationship between wrapper and inner is like the relationship between five fingers in one palm and four fingers. Take out the intermediate elements, write down the positions, and bond all the elements after processing. This is simple. The Code is as follows (to make the problem more general, I slightly changed the source string ):

Copy codeThe Code is as follows: <script type = "text/javascript">
Var str = "<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>

Texture:

Update

The comments are even more brilliant! Please refer to the Code in the comment:Copy codeThe Code is as follows: alert (sourcestr. replace (/<table .*? \/Table>/ig, function ($1) {return $1. replace (/<p>/ig, "<br> ")}));

PS: This site has just added the code parsing in the comments. You can paste the code. For the format, see the legend in the comment section. Thank you for your cooperation!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.