In JavaScript/JQuery I want to get all the text that is seen between some other text. For example, if the HTML document had:
在JavaScript / JQuery中,我想获得在其他一些文本之间看到的所有文本。例如,如果HTML文档具有:
<b class="blah">Blah: Some Text 1</b>
<div id="foo"><b class="blah">Blah: Some Text 2</b>
I'd like to get an array that has 'Some Text 1' and 'Some Text 2' since they are both in between '<b class="blah">Blah:
' followed by a '</b>
'
我想得到一个包含'Some Text 1'和'Some Text 2'的数组,因为它们都位于' Blah:'后跟' '之间
3 个解决方案
#1
Since you mention jQuery, just select all the right nodes and check their text. You can put a regex in here if you want, but it's not needed.
既然你提到了jQuery,只需选择所有正确的节点并检查它们的文本。如果你愿意,你可以在这里放一个正则表达式,但不需要它。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$( function(){
var texts = [];
$('b.blah').each( function()
{
var txt = $(this).text();
if ( 0 == txt.indexOf( 'Blah:' ) )
{
texts.push( txt.substr( 6 ) );
}
} );
alert( texts );
});
</script>
</head>
<body>
<b class="blah">Blah: Some Text 1</b>
<div id="foo"><b class="blah">Blah: Some Text 2</b>
<div id="foo"><b class="blah">Some Text 3</b>
</body>
</html>
Or with a string of HTML
或者使用一串HTML
$( function(){
var htmlChunk = '<b class="blah">Blah: Some Text 1</b>\n'
+ '<div id="foo"><b class="blah">Blah: Some Text 2</b></div>\n'
+ '<div id="foo2"><b class="blah">Some Text 3</b></div>';
var texts = [];
$('b.blah', '<div>' + htmlChunk + '</div>').each( function()
{
var txt = $(this).text();
if ( 0 == txt.indexOf( 'Blah:' ) )
{
texts.push( txt.substr( 6 ) );
}
} );
alert( texts );
});
#2
This is kind of hard in JS, because there's no handy way to retrieve a global set of paren captures. A hack like this might work:
这在JS中很难,因为没有方便的方法来检索全局的paren捕获。像这样的黑客可能有用:
var chunked = text.replace(/.*<b class="blah">(.*?)<\/b>/g, '$1|ARBITRARY_SEPARATOR|');
var chunks = chunked.split(/|ARBITRARY_SEPARATOR|/);
chunks.pop();
#3
This code will produce an array with the text between '<b class="blah">Blah:
' and '</b>
'. in this example 'Some Text 1' and 'Some Text 2'
此代码将生成一个数组,其中的文本介于' Blah:'和' '之间。在此示例中'Some Text 1'和'Some Text 2'
var s = '<b class="blah">Blah: Some Text 1</b><div id="foo"><b class="blah">Blah: Some Text 2</b>';
var regex = /<b class="blah">Blah: (.+?)<\/b>/gi;
var result = [];
var e;
while (e = regex.exec(s))
{
result.push(e[1]);
};
#1
Since you mention jQuery, just select all the right nodes and check their text. You can put a regex in here if you want, but it's not needed.
既然你提到了jQuery,只需选择所有正确的节点并检查它们的文本。如果你愿意,你可以在这里放一个正则表达式,但不需要它。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$( function(){
var texts = [];
$('b.blah').each( function()
{
var txt = $(this).text();
if ( 0 == txt.indexOf( 'Blah:' ) )
{
texts.push( txt.substr( 6 ) );
}
} );
alert( texts );
});
</script>
</head>
<body>
<b class="blah">Blah: Some Text 1</b>
<div id="foo"><b class="blah">Blah: Some Text 2</b>
<div id="foo"><b class="blah">Some Text 3</b>
</body>
</html>
Or with a string of HTML
或者使用一串HTML
$( function(){
var htmlChunk = '<b class="blah">Blah: Some Text 1</b>\n'
+ '<div id="foo"><b class="blah">Blah: Some Text 2</b></div>\n'
+ '<div id="foo2"><b class="blah">Some Text 3</b></div>';
var texts = [];
$('b.blah', '<div>' + htmlChunk + '</div>').each( function()
{
var txt = $(this).text();
if ( 0 == txt.indexOf( 'Blah:' ) )
{
texts.push( txt.substr( 6 ) );
}
} );
alert( texts );
});
#2
This is kind of hard in JS, because there's no handy way to retrieve a global set of paren captures. A hack like this might work:
这在JS中很难,因为没有方便的方法来检索全局的paren捕获。像这样的黑客可能有用:
var chunked = text.replace(/.*<b class="blah">(.*?)<\/b>/g, '$1|ARBITRARY_SEPARATOR|');
var chunks = chunked.split(/|ARBITRARY_SEPARATOR|/);
chunks.pop();
#3
This code will produce an array with the text between '<b class="blah">Blah:
' and '</b>
'. in this example 'Some Text 1' and 'Some Text 2'
此代码将生成一个数组,其中的文本介于' Blah:'和' '之间。在此示例中'Some Text 1'和'Some Text 2'
var s = '<b class="blah">Blah: Some Text 1</b><div id="foo"><b class="blah">Blah: Some Text 2</b>';
var regex = /<b class="blah">Blah: (.+?)<\/b>/gi;
var result = [];
var e;
while (e = regex.exec(s))
{
result.push(e[1]);
};