plus
plus

Reputation: 365

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:

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

Upvotes: 0

Views: 130

Answers (1)

Robert
Robert

Reputation: 2763

This should match content of all divs that contains only text. 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 ? And this catch all divs. if you post full example of content in witch you search i will update this regex to match exactly what you need. 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);
}

Upvotes: 1

Related Questions