简体   繁体   English

如何制作正则表达式以获取 div 中的字符串? REACT-NATIVE

[英]How to make a regex to get the string inside divs? REACT-NATIVE

im building a notebloc, and I have a problem is that the structure of this is using html, cause im using a component, the problem comes when I want to share the note, but i have image on it, so I just want to share the string, not the image, so when i want to share the content i get this:我正在构建一个 notebloc,我有一个问题是它的结构正在使用 html,因为我正在使用一个组件,当我想分享笔记时出现问题,但我有图像,所以我只想分享字符串,而不是图像,所以当我想分享内容时,我得到这个:

hola como estas&nbsp;<br><div><img src="file:///data/user/0/com.app/cache/ImagePicker/f91cb747-dc41-48c1-a6a0-c68907b8583b.jpg"></div><br><div>you es</div>

as you can see theres two part of string, the first one is hola como estas then is the structure for the image, it is inside a <div> and the comes the second string that is inside another <div> , so I want to get the string inside the <div> that only has string, not the div that has image, was thinking in many ways and the only one its using regex, but im not good with it, if anyone can help me如您所见,字符串有两个部分,第一个是hola como estas然后是图像的结构,它在<div>内,第二个字符串在另一个<div>内,所以我想获取只有字符串的<div>中的字符串,而不是有图像的 div,在很多方面都在思考,唯一一个使用正则表达式的方法,但我不擅长它,如果有人能帮助我

This should match content of all divs that contains only text.这应该匹配所有仅包含文本的 div 的内容。 Like in your example but i'm not sure that this is what you really what, because what if there is some p or a tags?就像在您的示例中一样,但我不确定这是否是您真正想要的,因为如果有一些 p 或 a 标签怎么办? And this catch all divs.这抓住了所有的div。 if you post full example of content in witch you search i will update this regex to match exactly what you need.如果您在 witch you search 中发布完整的内容示例,我将更新此正则表达式以完全匹配您的需要。 JSBin JSBin

const text = `hola como estas&nbsp;<br><div><img src="file:///data/user/0/com.app/cache/ImagePicker/f91cb747-dc41-48c1-a6a0-c68907b8583b.jpg"></div><br><div>you es</div>`

const regex = /<div>(?<text>(\w|\s)*)<\/div>/gi;

Array.from(text.matchAll(regex)).forEach((match) =>{
  console.log(match.groups.text, match.index)
})
const text = `hola como estas&nbsp;<br><div><img src="file:///data/user/0/com.app/cache/ImagePicker/f91cb747-dc41-48c1-a6a0-c68907b8583b.jpg"></div><br><div>you es</div>`

const regex = /<div>(?<text>(?:\w|\s)*)<\/div>/gi;

let result;
while ((result = regex.exec(text)) !== null) {
  console.log("Foundt: " + result[1] + "at index" + result.index);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM