使用正则表达式来修剪html [英] Using regular expression to trim html

查看:99
本文介绍了使用正则表达式来修剪html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要一个正则表达式去除下面例子中演示的html标签之间的换行符,制表符和空格:



来源:

 < html> 
< head>
< title>
某些标题
< / title>
< / head>
< / html>

想要的结果:

 < html>< head>< title>部分标题< / title>< / head>< / html> 

在Some title之前对空格进行修剪是可选的。
我会很感激任何帮助

解决方案

s / \s *( < [^> +>)\s * / \ 1 / gs



或者,在c# Regex.Replace(html,\s *(< [>] +>)\s *,$ 1 ,RegexOptions.SingleLine);


Been trying to solve this for a while now.

I need a regex to strip the newlines, tabs and spaces between the html tags demonstrated in the example below:

Source:

<html>
   <head>
     <title>
           Some title
       </title>
    </head>
</html>

Wanted result:

<html><head><title>Some title</title></head></html>

The trimming of the whitespaces before the "Some title" is optional. I'd be grateful for any help

解决方案

s/\s*(<[^>]+>)\s*/\1/gs

or, in c#:

Regex.Replace(html, "\s*(<[^>]+>)\s*", "$1", RegexOptions.SingleLine);

这篇关于使用正则表达式来修剪html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆