javascript - 在反应中显示字符串中两个子字符串之间的所有出现
问题描述
我正在尝试直接从具有超过 25 页重复信息的 div 中提取信息。例如,信息看起来像这样:
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...."
我想在“你的名字是:”和“你的年龄是:”之间提取多次出现的子字符串,以及年龄、电子邮件。
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>
我希望信息循环并显示所有值一次。帮助。
解决方案
由于数据已更改,我已经更新了我的答案......
如果是我,我会用“你的名字是:”分割 DIV 文本,然后运行这个正则表达式
^([\w ]+).*? Your Age is: (\d+).*?email is: ([\w@].*?) Your
对于每一行,它将提取您想要的数据。
在regex101.com上查看它。
如果您不想或不能先拆分字符串,那么您将在电子邮件部分中获得乐趣以实际获取电子邮件。有关一些示例,请参见此问题。