jQuery在字符串中搜索开始标记和结束标记

时间:2015-04-23 10:34:45

标签: javascript jquery xml soap

我在字段中有以下数据字符串,如何使用jquery仅检索从此标记“<?xml version="1.0"?>”开始到此标记</soap:Envelope>

的所有内容
mylog.log:[qa53jdn575dganqmnen5a6irh4;OWI5MjFhMmI0NDk2NmYzNGNlNjcxOTg4NGRmMDExZGM=;1] INFO 2015-04-20 12:21:00,584 e.t.g.n.crm 
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <soap:Body>
        <webservice>
            <sessiontoken>admin</sessiontoken>
            <name>test</name>
            <id>12345</id>
            <parameters>
                <parameter>
                    <key>e</key>
                    <value>test2</value>
                    <type>string</type>
                    <length>90</length>
                </parameter>
                <parameter>
                    <key>a</key>
                    <value>test1</value>
                    <type>string</type>
                    <length>50</length>
                </parameter>
                <parameter>
                    <key>n</key>
                    <value>12345</value>
                    <type>string</type>
                    <length>90</length>
                </parameter>
                <parameter>
                    <key>s</key>
                    <value>3</value>
                </parameter>
                <parameter>
                    <key>sf</key>
                    <value>test5</value>
                    <type>string</type>
                    <length>50</length>
                </parameter>
                <parameter>
                    <key>u</key>
                    <value>test6</value>
                    <type>string</type>
                    <length>50</length>
                </parameter>
            </parameters>
        </webservice>
    </soap:Body> 
</soap:Envelope> 

所以它会返回:

<?xml version="1.0"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body>
    <webservice>
        <sessiontoken>admin</sessiontoken>
        <name>test</name>
        <id>12345</id>
        <parameters>
            <parameter>
                <key>e</key>
                <value>test2</value>
                <type>string</type>
                <length>90</length>
            </parameter>
            <parameter>
                <key>a</key>
                <value>test1</value>
                <type>string</type>
                <length>50</length>
            </parameter>
            <parameter>
                <key>n</key>
                <value>12345</value>
                <type>string</type>
                <length>90</length>
            </parameter>
            <parameter>
                <key>s</key>
                <value>3</value>
            </parameter>
            <parameter>
                <key>sf</key>
                <value>test5</value>
                <type>string</type>
                <length>50</length>
            </parameter>
            <parameter>
                <key>u</key>
                <value>test6</value>
                <type>string</type>
                <length>50</length>
            </parameter>
        </parameters>
    </webservice>
</soap:Body>

3 个答案:

答案 0 :(得分:0)

var start = '<?xml version="1.0"?>';
var end = '</soap:Envelope>';

var text = 'mylog.log:[qa53jdn575dganqmnen5a6irh4;OWI5MjFhMmI0NDk2NmYzNGNlNjcxOTg4NGRmMDExZGM=;1] INFO 2015-04-20 12:21:00,584 e.t.g.n.crm <?xml version="1.0"?>...</soap:Envelope>';

var startIndex = text.indexOf(start);
var endIndex = text.indexOf(end);
var innerText = text.slice(startIndex, endIndex);

Here is the fiddle

答案 1 :(得分:0)

var longstring = 'mylog.log:[qa53jdn575dganqmnen5a6irh4;OWI5MjFhMmI0NDk2NmYzNGNlNjcxOTg4NGRmMDExZGM=;1] INFO 2015-04-20 12:21:00,584 e.t.g.n.crm <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <webservice> <sessiontoken>admin</sessiontoken> <name>test</name> <id>12345</id> <parameters> <parameter> <key>e</key> <value>test2</value> <type>string</type> <length>90</length> </parameter> <parameter> <key>a</key> <value>test1</value> <type>string</type> <length>50</length> </parameter> <parameter> <key>n</key> <value>12345</value> <type>string</type> <length>90</length> </parameter> <parameter> <key>s</key> <value>3</value> </parameter> <parameter> <key>sf</key> <value>test5</value> <type>string</type> <length>50</length> </parameter> <parameter> <key>u</key> <value>test6</value> <type>string</type> <length>50</length> </parameter> </parameters> </webservice> </soap:Body> </soap:Envelope>'

然后

var substring = longstring.substr(longstring.indexOf('<?xml'),longstring.length); 

答案 2 :(得分:-1)

你甚至不需要jquery。您可以使用常规表达来提取模式。

这样的东西
var yourString = "sdfsdfsdf ?xml version="1.0"?> </soap:Body>"
var x = /<?xml(.+)</soap:Body>+/;
var matches = x.match(yourString);