Searching for a sub-string (which changes each loop iteration), inside a big string

check if a string is a substring of another string in c
substring java
program to find substring in a string in c++
check if a string is a substring of another string in java
given two strings, find if first string is a subsequence of second
substring search
substring in c++
z algorithm

I am looping through HTML elements, and extracting the following string from a custom dataset.

"<div class="item"><span class="label">Test:</span><span>server</span></div><div class="item"><span <span class="label">Assigned to:</span><span>name(position) </span></div><div"

What I need to do, is extract the keyword name. The problem is, the name is essentialy a variable- each time the string appears in the loop, there is always the sentence Assigned to:</span><span>name(position), only the {{name}} changes but shows as a string. How do I search and extract the name? Do I look for Assigned to:</span><span> and copy the word afterwards? How do I do it?

Thanks!


Without knowing the full scope of your problem, it's hard to say – but it might be a good alternative to get the contents you want straight from the DOM instead of manually filtering strings. In many cases, this tends to be more robust and maintainable, compare to using regexes (which are great, anyway).

Document.querySelectorAll() and the Adjacent sibling combinator could be your friends here:

// fetch all spans which are neighbour to a span with the class 'label'
const targetSpans = document.querySelectorAll('span.label + span');
// Iterate the items, outputting each of their contents
targetSpans.forEach(target => {
  console.log(target.textContent);
});

Provided your code snippet, this would also match the span containing 'server' as a textContent. But if that's your only 'false positive', filtering it out should be pretty easy.

Like I said, with the provided information it's hard to say what solution solves your actual problem best. But DOM parsing might be an option as well.

Find if a given string can be represented from a substring by iterating , Given a string 'str', check if it can be constructed by taking a substring of it and In case of 'true' , the substring 'str[0..n-len-1]' is the substring that the loop calculates lps[i] for i = 1 to M-1 if string B exists as substring in string A � Minimum changes required to last_page Factorial of a large number. In this alternative approach a pointer is created to the parent string and then a pointer to a c-string array of characters is created with the original character used in the parent string. The parent string array of characters is then looped through and at each iteration a conditional check is made to determine if the current parent string


Your initial thought is correct - you need to search for the constant thing that will be surrounding your name in every string, and extract the name from within it.

From your question, it appears your name looks like this:

<span class="label">Assigned to:</span><span>name(position) </span>

The way that I would do this, is using a regular expression - I find the site https://regex101.com/ really useful to get them correct. Paste your whole string in there, then build the expression and ensure the part you want is being matched.

In this case, you want to have a regex something like this:

const regex = /<span class="label">Assigned to:<\/span><span>(.*?)<\/span>/

You can see how this looks on Regex 101 here (note the captured group):

The site also explains what each part of the regex does. In this case it's pretty much a straight text match (the \/ part is simply to escape the / character within the regex), and the only interesting part is that we're capturing everything within the match using a lazy quantifier, which means it won't capture more than it needs to. If we didn't use the lazy quantifier (the ?), it would match everything after the first span until the last closing span in the whole string, because the . character matches everything, so be careful of this (try adding an extra </span> onto your test string to see what I mean.

I'll leave it up to you to read up on how to implement a regex match in javascript.

Finding all positions of substring in a larger string in C#, Also the same using an iterator: To fix this just add +=1 in loop instead of += value. Change the matchString param to a Regex matchExpression in another public static int[] AllIndexesOf(string str, string substr, bool ignoreCase = false) { if� 0 Searching for a sub-string (which changes each loop iteration), inside a big string Nov 29 '19. 0 How to dynamically check existence of a nested object in global scope?


Complicated searches are generally better done with regex. This appears to do what you want:

    const subj='<div class="item"><span class="label">Test:</span><span>server</span></div><div class="item"><span><span class="label">Assigned to:</span><span>Fred(position) </span></div><div';
    const re=/Assigned to:<\/span><span>([^\(]*)/;
    const answers=re.exec(subj);
    console.log(answers[1]);

Were assigning the string to "subj", the regex search pattern to "re", then assigning the results of doing and exec in them into "z".

The regex itself: Starts with a slash, has the literal that prepends the name field.... and then the magic.

The parentheses are a "capture group"; whatever matches what's inside them will be returned separately. "^" is "not", the "\" is to escape the "(" that is a reserved character,"*" means "find as many as you can."

So were telling it to look for the prefix string, then return every character until we hit the "(".

Then the return handling. "exec" returns the entire match in [0]--not what we want. The first (only in this case) capture group is in [1].

4.3. Loops and Strings — AP CS Awesome, The first character in a Java String is at index 0 and the last characters is at length () - 1. String substring(int from, int to) : returns the substring beginning at index from Change the code to add code for a counter variable to count the number of 1's While loops are often used with strings when you are looking for a certain � In this post, we will discuss various methods to iterate over characters in a String in Java. 1. Naive. Naive solution would be to use a simple for loop to process each character of the String. This approach proves to be very effective for strings of smaller length.


Loops and Strings | Think Java, In this chapter, you'll learn how to use while and for loops to add repetition to your code. We'll also take a first look at String methods and solve some interesting problems. The body of the loop should change the value of one or more variables so At the end of each iteration, the update runs, and we go back to step 2. It yields an iterator which can can be used to iterate over all the columns of a dataframe. For each column in the Dataframe it returns an iterator to the tuple containing the column name and column contents as series. Let’s user iteritems() to iterate over the columns of above created Dataframe,


Strings and string manipulation in C++, when we declare a string variable without initialization (as in the example If we want to change all of them, we would need to know the length of the string. Thus, we could use that method to control a loop that allows us to convert all the for a substring), you can specify a starting position for the search; in that case, the� For-Each Loop is another form of for loop used to traverse the array. for-each loop reduces the code significantly and there is no use of the index or rather the counter in the loop. Syntax: For(<DataType of array/List><Temp variable name> : <Array/List to be iterated>){ System.out.println(); //Any other operation can be done with this temp


String.prototype.substring(), The function call at the end of the example changes the string Brave New World to Brave New Web . // Replaces oldS with newS in the string fullS� I am writing a piece of code that would need to iterate on the content of a string, each values being separated with a ,. e.g. I have my elements. v_list_pak_like varchar2(4000) := 'PEBO,PTGC,PTTL,PTOP,PTA'; How can I get it into an Array / Cursor to iterate on it in my loop? for x in (elements) loop -- do my stuff end loop;