javascript - js regular replacement problem
高洛峰
高洛峰 2017-07-05 10:55:03
0
2
909
<!DOCTYPE html>
<html>
<head>
    <title>内容</title>
</head>
<body>
中文
<p>内容<i>内容</i></p>
</body>
</html>

Replace the content in the tag with

<!DOCTYPE html>
<html>
<head>
    <title>{{#内容#}}</title>
</head>
<body>
{{#中文#}}
<p>{{#内容#}}<i>{{#内容#}}</i></p>
</body>
</html>

How to write the regular expression?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
Ty80

First, if you have learned the principles of compilation, you will know that regular expressions are not capable of processing nested data structures. In other words, if you want to achieve the requirement of [selecting the first i tag of nested p in body] through regular expression, it is not feasible in principle.

Second, you are dealing with a structured DOM text, so you can use jQuery’s parseHTML method to achieve this. The object obtained after passing jQuery parse can use $ to further select nodes such as p or i. This can solve your text replacement problem simply and effectively.

If you are on the Node server, just replace jQuery with cheerio.

Ty80

If you only want to replace in the current test text you gave, because the situation is relatively simple, you only need to write a regular match like ([u4e00-u9fa5]+) and replace it with {{#$1#}}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!