[英]Get Array of Strings In Between Two Strings with Javascript
This question has been asked a few times before, here's an example . 这个问题之前已经问过几次了,下面是一个例子 。 However, the question linked only asks about getting one string out of the result. 但是,链接的问题仅询问从结果中获取一个字符串。 The text I would like to parse has many different instances of the trailing and leading strings, and thus the code below does not work: 我想解析的文本具有结尾和开头字符串的许多不同实例,因此下面的代码不起作用:
test.match("SomeString(.*)TrailingString");
As shown in this fiddle . 就像这个小提琴所示。 I will show you the intended result below: 我将在下面显示您的预期结果:
If I were to have a string composed of the following elements STARTINGTEXTText I wantENDINGTEXT Text I don't want STARTINGTEXTMore text I wantENDINGTEXT Text I don't want
如果我要有一个由以下元素组成的字符串,则STARTINGTEXTText I wantENDINGTEXT Text I don't want STARTINGTEXTMore text I wantENDINGTEXT Text I don't want
I would like to have a function that I can pass in the arguments STARTINGTEXT
and ENDINGTEXT
and it would return an array with "Text I want"
and "More text I want"
我想要一个可以传递参数STARTINGTEXT
和ENDINGTEXT
,它将返回一个包含"Text I want"
和"More text I want"
"Text I want"
的数组
Thanks! 谢谢!
EDIT - This is a Pebble Application so JQuery isn't an option. 编辑 -这是一个Pebble应用程序,因此不能选择JQuery。
This similar thing has been done in Objective-C : 在Objective-C中也做了类似的事情:
-(NSMutableArray*)stringsBetweenString:(NSString*)start andString:(NSString*)end
{
NSMutableArray* strings = [NSMutableArray arrayWithCapacity:0];
NSRange startRange = [self rangeOfString:start];
for( ;; )
{
if (startRange.location != NSNotFound)
{
NSRange targetRange;
targetRange.location = startRange.location + startRange.length;
targetRange.length = [self length] - targetRange.location;
NSRange endRange = [self rangeOfString:end options:0 range:targetRange];
if (endRange.location != NSNotFound)
{
targetRange.length = endRange.location - targetRange.location;
[strings addObject:[self substringWithRange:targetRange]];
NSRange restOfString;
restOfString.location = endRange.location + endRange.length;
restOfString.length = [self length] - restOfString.location;
startRange = [self rangeOfString:start options:0 range:restOfString];
}
else
{
break;
}
}
else
{
break;
}
}
return strings;
}
If you would prefer a RegExp solution, you could do something like this: 如果您希望使用RegExp解决方案,则可以执行以下操作:
var test = "STARTINGTEXTText I wantENDINGTEXT Text I don't want STARTINGTEXTMore text I wantENDINGTEXT Text I don't want";
var matches = test.match(/STARTINGTEXT(.*?)ENDINGTEXT/g);
The key to this is the "g" (or global) flag, and the non-greedy repeat operator "*?". 这样做的关键是“ g”(或全局)标志,以及非贪婪的重复运算符“ *?”。 See this link for an explanation of the "g" flag and the non-greedy operator. 有关“ g”标志和非贪婪运算符的说明,请参见此链接 。
Here is a modification of your fiddle: link . 这是您的小提琴的修改: link 。 I changed it so that the alert would show a stringified JSON of the results, so that you could see it matching both strings. 我对其进行了更改,以便警报将显示结果的字符串化JSON,以便您可以看到匹配两个字符串的字符串。
This methodology uses very little code: 这种方法使用很少的代码:
function getBetweenText(fromString, ignoreStart, ignoreEnd){
var s = fromString.split(new RegExp(ignoreStart+'|'+ignoreEnd)), r = [];
for(var i=1,l=s.length; i<l; i+=2){
r.push(s[i]);
}
return r;
}
console.log(getBetweenText("STARTINGTEXTText I wantENDINGTEXT Text I don't want STARTINGTEXTMore text I wantENDINGTEXT Text I don't want", 'STARTINGTEXT', 'ENDINGTEXT'));
You can do this using jQuery. 您可以使用jQuery完成此操作。 To select all the elements with specific tag you just do something like this: ** UPDATED WITH NON-JQUERY VERSION ** 要选择带有特定标签的所有元素,您只需执行以下操作:**使用非JQUERY版本更新**
var HTMLelements = document.getElementsByTagName("tag");
var results = [];
for(var i = 0; i < HTMLelements.length; i++){
results.push(HTMLelements[i].innerHTML);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.