1

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

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"

Thanks!

EDIT - This is a Pebble Application so JQuery isn't an option.

This similar thing has been done in 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;

}
4
  • so you want to keep things between opening and closing tags, but not between closing and opening tags? Is there a reason you can't just use an XML parser (which you clearly need here, don't use regex for parsing markup) to throw away all "untagged" content? Commented May 3, 2016 at 0:03
  • The example I gave with tags wasn't a very good one. I'll add another to make it clearer. Commented May 3, 2016 at 0:04
  • You should start by using regex literals instead of string literals for regular expressions. Commented May 3, 2016 at 0:13
  • @AlexWulff never show "something like" what you're working with. Show what you're working with =) Commented May 3, 2016 at 0:59

3 Answers 3

2

If you would prefer a RegExp solution, you could do something like this:

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 "*?". See this link for an explanation of the "g" flag and the non-greedy operator.

Here is a modification of your fiddle: 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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'));

1 Comment

Thank you - this is also helpful because it gets rid of the starting and ending strings
0

You can do this using jQuery. To select all the elements with specific tag you just do something like this: ** UPDATED WITH NON-JQUERY VERSION **

var HTMLelements = document.getElementsByTagName("tag");
var results = [];
for(var i = 0; i < HTMLelements.length; i++){
  results.push(HTMLelements[i].innerHTML);
}

6 Comments

Sorry - I forgot to mention that this is for a Pebble application and thus I can't use JQuery. My mistake!
Updated with plain HTML
Is there a way to acomplish this where you can supply a unique ending tag?
Where the beginning string and ending string are different.
I am not quiet sure what do you mean by that. Do you have an example?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.