什么样选拔Javascript正则表达式来格式化XML内容,

作者: 韦德国际1946国际网址  发布:2019-05-28

图片 1图片 2用正则式格式化XML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

动用得是Emeditor ,在看XML文书档案时,总是因为格式混乱而看不清。这么些是3个Emeditor宏来自动格式化XML。下载:formatXml.rar (1,021.00 bytes)
上面这段是其一网页版的javascript格式化XML的代码。

行使得是Emeditor ,在看XML文书档案时,总是因为格式混乱而看不清。这一个是贰个Emeditor宏来自动格式化XML。下载:formatXml.rar(1,021.00 bytes)
上面这段是那个网页版的javascript格式化XML的代码。

小说来给咱们介绍在Javascript 正则表明式使用变量时部分难题与缓和格局,主要讲到的更迭与RegExp二种方法

<html>
<head>
    <meta http-equiv=content-type content="text/html; charset=UTF-8">
    <title>Xml格式化学工业具</title>
    <script type="text/javascript">
    String.prototype.removeLineEnd = function()
    {
        return this.replace(/(<. ?s ?)(?:ns*?(. ?=".*?"))/g,'$1 $2')
    }
    function formatXml(text)
    {
        //去掉多余的空格
        text = 'n'   text.replace(/(<w )(s.*?>)/g,function($0, name, props)
        {
            return name   ' '   props.replace(/s (w =)/g," $1");
        }).replace(/>s*?</g,">n<");
        
        //把注释编码
        text = text.replace(/n/g,'r').replace(/<!--(. ?)-->/g,function($0, text)
        {
            var ret = '<!--'   escape(text)   '-->';
            //alert(ret);
            return ret;
        }).replace(/r/g,'n');
        
        //调度格式
        var rgx = /n(<(([^?]). ?)(?:s|s*?>|s*?(/)>)(?:.*?(?:(?:(什么样选拔Javascript正则表达式来格式化XML内容,使用Javascript正则表明式来格式化XML内容。/)>)|(?:<(/)2>)))?)/mg;
        var nodeStack = [];
        var output = text.replace(rgx,function($0,all,name,isBegin,isCloseFull1,isCloseFull2 ,isFull1,isFull2){
            var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');
            //alert([all,isClosed].join('='));
            var prefix = '';
            if(isBegin == '!')
            {
                prefix = getPrefix(nodeStack.length);
            }
            else 
            {
                if(isBegin != '/')
                {
                    prefix = getPrefix(nodeStack.length);
                    if(!isClosed)
                    {
                        nodeStack.push(name);
                    }
                }
                else
                {
                    nodeStack.pop();
                    prefix = getPrefix(nodeStack.length);
                }

复制代码 代码如下:

复制代码 代码如下:

例一,replace带有变量

            
            }
                var ret =  'n'   prefix   all;
                return ret;
        });
        
        var prefixSpace = -1;
        var outputText = output.substring(1);
        //alert(outputText);
        
        //把注释还原并解码,调格式
        outputText = outputText.replace(/n/g,'r').replace(/(s*)<!--(. ?)-->/g,function($0, prefix,  text)
        {
            //alert(['[',prefix,']=',prefix.length].join(''));
            if(prefix.charAt(0) == 'r')
                prefix = prefix.substring(1);
            text = unescape(text).replace(/r/g,'n');
            var ret = 'n'   prefix   '<!--'   text.replace(/^s*/mg, prefix )   '-->';
            //alert(ret);
            return ret;
        });
        
        return outputText.replace(/s $/g,'').replace(/r/g,'rn');

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv=content-type content="text/html; charset=UTF-8">
    <title>Xml格式化学工业具</title>
    <script type="text/javascript">
    String.prototype.removeLineEnd = function()
    {
        return this.replace(/(<. ?s ?)(?:ns*?(. ?=".*?"))/g,'$1 $2')
    }
    function formatXml(text)
    {
        //去掉多余的空格
        text = 'n' text.replace(/(<w )(s.*?>)/g,function($0, name, props)
        {
            return name ' ' props.replace(/s (w =)/g," $1");
        }).replace(/>s*?</g,">n<");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv=content-type content="text/html; charset=UTF-8">
    <title>Xml格式化工具</title>
    <script type="text/javascript">
    String.prototype.removeLineEnd = function()
    {
        return this.replace(/(<. ?s ?)(?:ns*?(. ?=".*?"))/g,'$1 $2')
    }
    function formatXml(text)
    {
        //去掉多余的空格
        text = 'n' text.replace(/(<w )(s.*?>)/g,function($0, name, props)
        {
            return name ' ' props.replace(/s (w =)/g," $1");
        }).replace(/>s*?</g,">n<");

replace函数能够运用正则表明式来相配字符串完毕替换。

    }

        //把注释编码
        text = text.replace(/n/g,'r').replace(/<!--(. ?)-->/g,function($0, text)
        {
            var ret = '<!--' escape(text) '-->';
            //alert(ret);
            return ret;
        }).replace(/r/g,'n');

        //把注释编码
        text = text.replace(/n/g,'r').replace(/<!--(. ?)-->/g,function($0, text)
        {
            var ret = '<!--' escape(text) '-->';
            //alert(ret);
            return ret;
        }).replace(/r/g,'n');

前天遇上的难题是正则表达式出现了变量,以后天的其实际景况况为例:

    function getPrefix(prefixIndex)
    {
        var span = '    ';
        var output = [];
        for(var i = 0 ; i < prefixIndex;  i)
        {
            output.push(span);
        }
        
        return output.join('');
    }        
        function btnFormat_click()
        {
            var $ = document.getElementById;
            $('output').value = formatXml($('input').value);
        }
        
    </script>
    
    
</head>

        //调治格式
        var rgx = /n(<(([^?]). ?)(?:s|s*?>|s*?(/)>)(?:.*?(?:(?:(/)>)|(?:<(/)2>)))?)/mg;
        var nodeStack = [];
        var output = text.replace(rgx,function($0,all,name,isBegin,isCloseFull1,isCloseFull2 ,isFull1,isFull2){
            var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');
            //alert([all,isClosed].join('='));
            var prefix = '';
            if(isBegin == '!')
            {
                prefix = getPrefix(nodeStack.length);
            }
            else
            {
                if(isBegin != '/')
                {
                    prefix = getPrefix(nodeStack.length);
                    if(!isClosed)
                    {
                        nodeStack.push(name);
                    }
                }
                else
                {
                    nodeStack.pop();
                    prefix = getPrefix(nodeStack.length);
                }

        //调解格式
        var rgx = /n(<(([^?]). ?)(?:s|s*?>|s*?(/)>)(?:.*?(?:(?:(/)>)|(?:<(/)2>)))?)/mg;
        var nodeStack = [];
        var output = text.replace(rgx,function($0,all,name,isBegin,isCloseFull1,isCloseFull2 ,isFull1,isFull2){
            var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');
            //alert([all,isClosed].join('='));
            var prefix = '';
            if(isBegin == '!')
            {
                prefix = getPrefix(nodeStack.length);
            }
            else
            {
                if(isBegin != '/')
                {
                    prefix = getPrefix(nodeStack.length);
                    if(!isClosed)
                    {
                        nodeStack.push(name);
                    }
                }
                else
                {
                    nodeStack.pop();
                    prefix = getPrefix(nodeStack.length);
                }

 代码如下

<body>
    <textarea id="input" style="width:100%;height:45%;"></textarea>
    <div style="margin:0px auto;"><input type="button" id="btnFormat" onclick="btnFormat_click()" value="格式化" style="width:50%;height:10%;"/></div>
    <textarea id="output" style="width:100%;height:45%;"></textarea>

            }
                var ret =  'n' prefix all;
                return ret;
        });

            }
                var ret =  'n' prefix all;
                return ret;
        });

复制代码

</body>
</html>

        var prefixSpace = -1;
        var outputText = output.substring(1);
        //alert(outputText);

        var prefixSpace = -1;
        var outputText = output.substring(1);
        //alert(outputText);

/(^' arr2[i] '|)|(|' arr2[i] '$)/


        //把注释还原并解码,调格式
        outputText = outputText.replace(/n/g,'r').replace(/(s*)<!--(. ?)-->/g,function($0, prefix,  text)
        {
            //alert(['[',prefix,']=',prefix.length].join(''));
            if(prefix.charAt(0) == 'r')
                prefix = prefix.substring(1);
            text = unescape(text).replace(/r/g,'n');
            var ret = 'n' prefix '<!--' text.replace(/^s*/mg, prefix ) '-->';
            //alert(ret);
            return ret;
        });

        //把注释还原并解码,调格式
        outputText = outputText.replace(/n/g,'r').replace(/(s*)<!--(. ?)-->/g,function($0, prefix,  text)
        {
            //alert(['[',prefix,']=',prefix.length].join(''));
            if(prefix.charAt(0) == 'r')
                prefix = prefix.substring(1);
            text = unescape(text).replace(/r/g,'n');
            var ret = 'n' prefix '<!--' text.replace(/^s*/mg, prefix ) '-->';
            //alert(ret);
            return ret;
        });

arr2[i]为数组成分,是变量

        return outputText.replace(/s $/g,'').replace(/r/g,'rn');
    }
    function getPrefix(prefixIndex)
    {
        var span = '    ';
        var output = [];
        for(var i = 0 ; i < prefixIndex; i)
        {
            output.push(span);
        }

        return outputText.replace(/s $/g,'').replace(/r/g,'rn');
    }
    function getPrefix(prefixIndex)
    {
        var span = '    ';
        var output = [];
        for(var i = 0 ; i < prefixIndex; i)
        {
            output.push(span);
        }

str=str.replace(/(^' arr2[i] '|)|(|' arr2[i] '$)/,'');

        return output.join('');
    }       
        function btnFormat_click()
        {
            var $ = document.getElementById;
            $('output').value = formatXml($('input').value);
        }

        return output.join('');
    }       
        function btnFormat_click()
        {
            var $ = document.getElementById;
            $('output').value = formatXml($('input').value);
        }

采纳这种方法不可能交替成功,消除办法:

    </script>

    </script>

 代码如下

   
</head>
<body>
    <textarea id="input" style="width:100%;height:45%;"></textarea>
    <div style="margin:0px auto;"><input type="button" id="btnFormat" onclick="btnFormat_click()" value="格式化" style="width:50%;height:10%;"/></div>
    <textarea id="output" style="width:100%;height:45%;"></textarea>
</body>
</html>

   
</head>
<body>
    <textarea id="input" style="width:100%;height:45%;"></textarea>
    <div style="margin:0px auto;"><input type="button" id="btnFormat" onclick="btnFormat_click()" value="格式化" style="width:50%;height:10%;"/></div>
    <textarea id="output" style="width:100%;height:45%;"></textarea>
</body>
</html>

复制代码

,在看XML文书档案时,总是因为格式混乱而看不清。这几个是2个Emeditor宏来自动格式化XML。下载: formatXml.rar (一,0二1.00 bytes) 上面那...

您也许感兴趣的文章:

  • Java/Js下利用正则表明式匹配嵌套Html标签
  • Javascript中正则表明式的全局相称形式分析
  • Javascript中使用exec举办正则表明式全局相配时的注意事项
  • javascript中万分价格的正则表明式
  • javascipt相配单行和多行注释的正则表达式
  • java正则表明式相配网页全部网站和链接文字的言传身教
  • java使用正则表明式剖断邮箱格式是还是不是科学的点子
  • Java正则表达式相配电话格式

var reg=eval_r('/(^' arr2[i] '|)|(|' arr2[i] '$)/');
str=str.replace(reg,'');

本文由韦德国际1946发布于韦德国际1946国际网址,转载请注明出处:什么样选拔Javascript正则表达式来格式化XML内容,

关键词: bv1946伟德国际