2

我正在尝试将其中包含 html 和非 html 的字符串分成两个字符串。

我的javascript字符串是:

<span>1x</span> Front-line zero tolerance productivity

我希望将其分为两个变量

var quantity = "1x";
var name = "Front-line zero tolerance productivity";
4

7 回答 7

5

<span>找到或</span>标记时拆分。

string = "<span>1x</span> Front-line zero tolerance productivity"

tokens = string.split(/<\/?span>/);  // returns ["", "1x", " Front-line zero tolerance productivity"]
var quantity = tokens[1]  // "1x"
var name = tokens[2];     // "Front-line zero tolerance productivity" 
于 2012-06-12T22:00:53.000 回答
3

鉴于您将始终拥有这种类型的标记,最简单的方法是使用jQuery 。contents()

HTML

<div id="split-me">
    <span>1x</span> Front-line zero tolerance productivity
</div>

jQuery

var $contents = $("#split-me").contents();
var quantity = $contents.eq(1).text();
var name = $contents.eq(2).text();

例子

于 2012-06-12T22:07:02.207 回答
2

假设它<span>1x</span> Front-line zero tolerance productivity被包裹在一个像下面这样的 div 中,

<div id="test">
    <span>1x</span> Front-line zero tolerance productivity
</div>

JS:

var $clonedTest = $('#test').clone();
var $span = $clonedTest.find('span');
var quality = $span.text();
$span.remove();
var name = $.trim($clonedTest.text());

演示:http: //jsfiddle.net/skram/Agfyk/2/

于 2012-06-12T22:00:12.147 回答
0

这可能不是很优雅,但您可以.replace()为第二个 var 使用 javascript 函数,例如:

$(document).ready(function(){
    $('#outp').html('first: ' + $('#inp span').html() + '<br/>' +
                    'second: ' + $('#inp').html().replace($('#inp span').html(),''));
});
于 2012-06-12T22:10:58.707 回答
0

这是一个更加动态和模块化的功能,可以获取您想要的所有潜文本块

jQuery.fn.extend({
    texts: function() {
        var texts = [];
        var blocks = this.contents();
        $.each(blocks, function(i, block) {
            if(block.nodeName === '#text') {
                var data = $.trim(block.data);
                data && texts.push(data);
            } else {
                texts = jQuery.merge(texts, $(block).texts());
            }
        });
        return texts;
    }
});

$(function() {
    console.debug($('<span><span>1x</span> Front-line zero tolerance productivity</span>').texts());            
});
于 2012-06-12T22:15:57.960 回答
0

Since the format of the string won't change, you can split on the first space to get the two parts. Then remove tags from the first returned element, and preserve the structure of the second half:

var str = "<span>1x</span> Front-line zero tolerance productivity";
var ndx = str.indexOf(" ");
var rst = []; // Array of results

rst.push( str.substr( 0,ndx ).replace(/<\/?.+?>/g,"") );
rst.push( str.substr( ndx+1 ) );

Which results in the following array:

["1x", "Front-line zero tolerance productivity"]

Fiddle: http://jsfiddle.net/jonathansampson/dBTKZ/

于 2012-06-12T21:59:43.750 回答
0
<div class="container">
    <span>1x</span> Front-line zero tolerance productivity
</div>

<script type="text/javascript">
var matches = $('.container').html().match(/<span>(.*)<\/span> ?(.*)/);

var spanText = matches[1];   // 1x
var textNode = matches[2];   // Front-line zero tolerance productivity
</script>
于 2012-06-12T22:37:20.680 回答