-2

I am trying to pull out information directly from a div which has more than 25 pages of repeating information. For instance, the information would look something like this:

var first = "Your Name is: name Your Age is: age Your email is: email....Your Name is: name Your Age is: age Your email is: email....Your Name is: name Your Age is: age Your email is: email....Your Name is: name Your Age is: age Your email is: email....Your Name is: name Your Age is: age Your email is: email...." 

I want to extract multiple occurrences of the substrings in between "Your Name is:" and "Your Age is:", as well as for age, email.

var name = "Your Name is:"
var age = "Your Age is:"
var re = new RegExp(name,'gi');
var re2 = new RegExp(age,'gi');


var result = new Array();
while (re.exec(first){
results.push(re.lastIndex);}
var result2 = new Array();
while (re2.exec(first){
results.push(re2.lastIndex);}

var info = first.substring(
first.lastIndexOf(name) + 1,
first.lastIndexOf(age)
);


<span>{info}</span>

I want info to loop and display all values once. Help.

Felix
  • 2,531
  • 14
  • 25
Bin
  • 19
  • 5

1 Answers1

0

Since the data was changed, I've updated my answer...

If it were me, I would split the DIV text by "Your Name is: " and then run this regex

^([\w ]+).*? Your Age is: (\d+).*?email is: ([\w@].*?) Your

For each line, it will pull the data you want.

See it working on regex101.com.

If you don't want to or can't split the string first, you're going to have fun with the email portion to actually get the email. See this question for some examples.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • I must explain that ... meant continous data inputs, there are way more data than name and age, thus ... I'm simply looking for a way to push an array of strings. – Bin Apr 07 '21 at 21:06
  • You must include that in your description. When you put an example, people are going to reasonably expect that it's an actual, realistic example of the text you want the regex to work on. We can't read your mind. Put at least 2 representative lines of text in your question as an example so we can see what the text actually looks like. If you need to change names, data, etc., use mock data instead. Use John Smith as the name, put a valid value for age, use something like email@email.com for the email, etc. – JeffC Apr 07 '21 at 21:18
  • I've updated the answer to work given your new description of the data – JeffC Apr 07 '21 at 21:24
  • I will remember that, regardless of lack of information. Your suggestion might just work, I will look more into regular expressions. But I still don't comprehend how to display array of substring within a string in react. – Bin Apr 07 '21 at 21:24